Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

NullReferenceException From Public Variable of Instantiated Object

Discussion in 'Scripting' started by Tavarin, Jul 12, 2019.

  1. Tavarin

    Tavarin

    Joined:
    Jun 13, 2019
    Posts:
    2
    In my game when the player uses one of their tools I instantiate the appropriate tool from a Resources folder on top of the player. I then call a function within the tool's script to change the sprite of the tool. To have fewer scripts I wrote a single tool script with the sprites passed into a public array, so I can use the same script for each different tool.

    If the tool already exists in the scene then the sprite changes just fine. But if I instantiate it from the resources folder then I get a NullReferenceException from the sprite array. But the instantiated object when inspected has the sprites declared in it's public array.

    Here is the relevant code I am using:

    Player Code:
    Code (CSharp):
    1. GameObject toolObject = Instantiate(Resources.Load(selectedItem.id, typeof(GameObject)), target, Quaternion.identity) as GameObject;
    2. ToolScript toolScript = GameObject.FindGameObjectWithTag("Tool").GetComponent<ToolScript>();
    3. toolScript.ChargeSprite(directionFacing);
    Tool Script Code:
    Code (CSharp):
    1. public Sprite[] sprites = new Sprite[8];
    2. SpriteRenderer rend;
    3.  
    4. void Start()
    5. {
    6.     rend = GetComponent<SpriteRenderer>();
    7. }
    8.  
    9. public void ChargeSprite(string direction)
    10. {
    11.     switch (direction)
    12.         {
    13.             case "Down": rend.sprite = sprites[1]; break;
    14.             case "Left": rend.sprite = sprites[2]; break;
    15.             case "Right": rend.sprite = sprites[5]; break;
    16.             case "Up": rend.sprite = sprites[7]; break;
    17.         }
    18.  }
    Does anyone know why the sprite array is null when the object is instantiated from the resources folder, versus already in the scene?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,695
    Stuff in the scene that reference other things in the scene maintain a reference.

    If you then instantiate a prefab from that object, it doesn't have a reference to the object.

    Think of it this way. Stuff in the scene is already instantiated, so when you connect stuff there, you're connecting stuff to an instance of that object.

    Prefabs are more like blueprints that don't really have a way of knowing what's in the scene when you spawn a copy, so it doesn't maintain connections to the stuff in the scene.
     
  3. Tavarin

    Tavarin

    Joined:
    Jun 13, 2019
    Posts:
    2
    Thanks, but the sprites in the prefab object are not from the scene, but are stored in the sprites folder, so should maintain a connection. I was actually able to the the script to stop throwing an null exception by moving the rend = GetComponant<SpriteRenderer>(); line into each of the functions.

    Now the issue I'm having is the sprite renderer just won't change the sprite during runtime, even if I do it from the inspector.