Search Unity

Help Setting Var on Start

Discussion in 'Scripting' started by wxrg66, Apr 8, 2021.

  1. wxrg66

    wxrg66

    Joined:
    Apr 3, 2021
    Posts:
    17
    I have a bit of code that allows a player to take a pickup object into their inventory, however i cant get the start function to work, it keeps turning up null.
    The null reference is to the Inventory variable which isnt being set upon start
    here's the code


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour
    6. {
    7.     public Inventory inventory;
    8.     public GameObject itemButton;
    9.  
    10.  
    11.     public void Start()
    12.     {
    13.      
    14.        inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    15.     }
    16.  
    17.  
    18.  
    19.     private void OnTriggerEnter2D(Collider2D other)
    20.     {
    21.         if (other.name == "Player") {
    22.          
    23.             // spawn the sun button at the first available inventory slot !
    24.          
    25.  
    26.             for (int i = 0; i < inventory.slots.Length; i++)
    27.             {
    28.                 if (inventory.isFull[i] == false) { // check whether the slot is EMPTY
    29.                  
    30.                     inventory.isFull[i] = true; // makes sure that the slot is now considered FULL
    31.                     Instantiate(itemButton, inventory.slots[i].transform, false); // spawn the button so that the player can interact with it
    32.                     Destroy(gameObject);
    33.                     break;
    34.                 }
    35.             }
    36.         }
    37.      
    38.     }
    39. }
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
     
    Last edited: Apr 8, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Code (CSharp):
    1. GetComponent<Inventory>();
    This code will return null if the Pickup script and the Inventory script are not on the same GameObject. If they're not on the same GameObject, this is not the right way to assign your variable.
     
    wxrg66 likes this.
  3. wxrg66

    wxrg66

    Joined:
    Apr 3, 2021
    Posts:
    17
    sorry i realized this after posting, i changed it but it still wont load the Inventory script on start
    Code (CSharp):
    1. private void Start()
    2.     {
    3.      inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    4.     }
    5.    
     
  4. wxrg66

    wxrg66

    Joined:
    Apr 3, 2021
    Posts:
    17
    I fixed it by changing FindGameObjectWithTag to Gameobject.Find.("Player").GetComponent<Inventory>();
     
  5. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    354
    I highly recommend avoid using strings, especially names, it can easily cause mistakes if you made typo for example. Make script called Player and search for player with FindObjectOfType<Player>(), in that case method will return Player script instead of GameObject and compiler will help dealing with typos. It will be ten times more reliable.
    You can also catch some errors on start with thing like that:
    Code (CSharp):
    1. var player = FindObjectOfType<Player>();
    2. if (player != null && player.TryGetComponent(out Inventory inv))
    3. {
    4.     inventory = inv;
    5. }
    6. else
    7. {
    8.     Debug.LogError("There is no player or it doesn't have an inventory)";
    9. }
     
    Last edited: Apr 8, 2021
    wxrg66 likes this.
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Just drag the Player gameobject with the Inventory script into the Inventory slot (because it's public), then you can access everything in the script right away. No Find or GetComponent is needed, dragging it in sets the reference. Only do that if it's private.
     
  7. wxrg66

    wxrg66

    Joined:
    Apr 3, 2021
    Posts:
    17
    I thought thatd be a good fix too, however when instantiating the items back into the world from the inventory they wont be set to the players inventory and wont be picked up, it wasnt picking up on the player tag for some reason and changing it to Find fixed this issue
     
  8. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Ah, gotcha. I wasn't seeing the whole picture. Glad you got it fixed.
     
    wxrg66 likes this.