D
D
DeadDeveloper2019-08-02 21:51:07
Unity
DeadDeveloper, 2019-08-02 21:51:07

GetComponent(); Explain?

In general, now for some reason in any C # script in Unity it does not work. GetComponent<GameObject>();
The script hangs on the Cube, the object is on the game stage, as this function stopped working, the script did. But the value of the variable is still null.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestGetComponent : MonoBehaviour {
    private GameObject HeroObject;
    private void Start()
    {
        HeroObject = GetComponent<GameObject>();
    }
    private void Update()
    {
        Debug.Log(HeroObject);
    }
}

PS Correction, it does not work exactly HeroObject = GetComponent<GameObject>();
If you write Then everything is ok. Or if for example:HeroObject = this.GameObject;
private Animator ch_anim;
private void Start()
{
ch_anim = GetComponetn<Animator>();
}

that's ok too.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question