- Home /
Why is gameObject == null?
Hi, I'm facing a problem with my code. I'm calling a function from another script. However when I try to use gameObject it is null. I tried to make a public Sprite Renderer and set a reference in Unity and by code in the Start method. But it returns null as well. Can you help me here?
public class ChangeSprite : MonoBehaviour
{
public Sprite[] spriteArray;
public SpriteRenderer spriteRenderer;
private void Start()
{
Debug.Log("Started");
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
Debug.Log(spriteRenderer);
}
public void changeSprite(int diceNumber)
{
Debug.Log(gameObject == null);
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
Debug.Log(spriteRenderer == null);
spriteRenderer.sprite = spriteArray[diceNumber];
}
}
I'm calling changeSprite from another script. Thanks in advance
How are you calling this from the other script? Where does your other script get the reference to this ChangeSprite script? If gameObject is null then that means that you are getting some weird detached version of this script.
I already changed my code around the problem. I think I called it like this: ChangeSprite changeSecSprite = new ChangeSprite(); int rolledNummber = rnd.Next(1,7); changeSecSprite.changeSprite(rolledNumber
Don't create a MonoBehaviour with new
. It makes no sense and leads to this kind of situations. The MonoBehaviour instance you create with new
has no gameObject
, and it won't execute its Start()
and Update()
methods. Because to have a gameObject
and to execute Start()
and other MonoBehaviour methods the MonoBehaviour must be attached to some GameObject.
To get this script working you need to attach it to the object which you want to change. Either manually attach it in the editor, or - if you have these objects created dynamically at runtime - you'll need to add the script to them right after their creation with myNewSpriteObject.gameObject.AddComponent<ChangeSprite>();
The script is already attached to a game object. So I should make a reference like
changeSecSprite = Game object.Find("Game object the change script is attached to").GetComponent<ChangeSprite>();
Yeah, like that. The thing is when you did that with new
you created a new ChangeSprite
instance that is not connected to any game object, and it had no gameObject
reference. And you should get the concrete instance of type ChangeSprite
that is actually attached to the game object, and you should do this with GetComponent
.
Answer by polarfuchs23 · Mar 04, 2021 at 12:48 PM
Edit: Ermiq found the mistake. I created a new instance of ChangeSprite in the script I called the function in. ( ChangeScript changeSecSprite = new ChangeScript;
) By this I only called the function changeSprite and skipped the start and update method. The fix is to grab the script via the game object like:
ScriptName scriptName = GameObject.Find("GameobjectItsAttachedTo").GetComponent<ScriptName>();
So in my case:
ChangeScript changeSecSprite = GameObject.Find("Dice").GetComponent<ChangeScript>();
Or by instantly calling the function from this:
GameObject.Find("Dice").GetComponent<ChangeScript>().changeSprite(rolledNumber);
Thanks a lot!!