GetComponent function returning null

天涯浪子 提交于 2021-02-05 06:16:25

问题


I'm developing a simple game in Unity 2017 in C#.

In my levels menu I have a Text object with a button component in it, and a script attached to it.

This is what's currently in the script:

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

public class LevelLock : MonoBehaviour {

    public bool isLocked = true;
    private Button lvlBtn;

    // Use this for initialization
    void Start () {
        lvlBtn = GetComponent<Button> ();
        if (lvlBtn != null) {
            Debug.Log ("this should be working");
        } else {
            Debug.Log ("you did something wrong");
        }
        //if(isLocked){
        //  lvlBtn.enabled = false;
        //}
    }

}

The problem is, that GetComponent<Button>() is returning null, meaning it's not finding the button component in the object.

I tried adding an Animator component and perform the same operation just to check and it did work, so I have no clue why it shouldn't find the button component. If anyone could help with this or point out to me something i'm doing wrong, I would really appreciate it

EDIT:

Inspector tab:

Console tab:


回答1:


Your screenshot shows that you attached the LevelLock script to the Button which is fine.

Possible reasons and fixes:

1.The component/script is not even attached to the GameObject or is attached to the wrong Object.

In, your code, calling GetComponent<Button>(); without finding the GameObject first means that GetComponent should look for a Button component that is attached to this GameObject this script(LevelLock) is attached to.

If the script you want to retrieve, is attached to another GameObject, you can solve this by first, finding the GameObject the Button component is attached to with GameObject.Find then retrieving the Button component/script from it.

Replace

lvlBtn = GetComponent<Button> ();

with

GameObject obj = GameObject.Find("lvl 2");
lvlBtn = obj.GetComponent<Button>();

3.Script is attached to multiple Objects

One of the reasons GetComponent<Button>() might return false is because you mistakenly attached the LevelLock script to another GameObject. It could be an empty GameObject but that GameObject doesn't have the Button component attached to it.

Find this GameObject and remove the LevelLock script from it.

To do that, select the LevelLock script from the Project tab then Go to Assets ---> Find References In Scene. Remove the LevelLock script from other GameObjects.

This will make sure that the LevelLock script is only attached to the "lvl 2" GameObject


3.Multiple scripts with the-same name but in different namespace:

If you have two classes with the-same name but in different namespace and you attached one to the GameObject while trying to access the other one from code, it will be null since they are not the-same. I looked into your project and this was the Problem.

You created a script named "Button" while Unity comes with it's own built-in script/component also named "Button" which is in the UnityEngine.UI namespace.

You attached the Unity's built-in "Button" script to your GameObject but then you are trying to access your own made "Button" script from the script. Supply the namespace which is UnityEngine.UI so that Unity will know that you want to use the Button component that comes with Unity.

Change:

vlBtn = GetComponent<Button> ();

to

lvlBtn = GetComponent<UnityEngine.UI.Button>();

Also change:

private Button lvlBtn;

to

private UnityEngine.UI.Button lvlBtn;

Finally, do not name your script the-same name as any built-in component like Button, Image, Text, Rigidbody and so on. Create a class name and make sure that it doesn't exist with Unity API otherwise you'll continue to run into such issues and even people on SO won't be able to help you unless they have access to your project.



来源:https://stackoverflow.com/questions/49827358/getcomponent-function-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!