Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

How do I interact with instantiated prefabs?

Discussion in '2D' started by snoots, Jun 2, 2021.

  1. snoots

    snoots

    Joined:
    Mar 14, 2021
    Posts:
    6
    Hey guys! I'm pretty new to unity, and I am having problems with instantiated trigger events. I know that the reason why it doesn't interact is because it returns a null, but I have no idea how to fix it. If anyone could help guide me to the right direction, that would be greatly appreciated!

    Instantiated prefab script (It spawns the TreeBranch, but doesn't interact with it):
    Code (CSharp):
    1.     public GameObject TreeBranch;
    2.  
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         TreeBranch = Instantiate(TreeBranch, transform.position = new Vector3(1.5f, 10, 0), transform.rotation);
    7.     }
    Interacting script:
    Code (CSharp):
    1.  private float branchDownNumb;
    2.     private GameObject treeBranch;
    3.     private bool playerFlipped;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         treeBranch = GameObject.Find("TreeBranch");
    9.         branchDownNumb = 2;
    10.         playerFlipped = false;
    11.     }
    12.  
    13.     private void OnTriggerEnter2D(Collider2D collision) {
    14.         if(collision.name == "TreeBranch") {
    15.             SceneManager.LoadScene("GameEndScene");
    16.         }
    17.     }
    18.  
    19.     void lumberJackController() {
    20.         if(Input.GetKeyDown(KeyCode.LeftArrow)) {
    21.             treeBranch.transform.Translate(new Vector3(0,-branchDownNumb,0));
    22.             ScoreScript.scoreValue += 1;
    23.             playerFlipped = false;
    24.         }
    25.         if(Input.GetKeyDown(KeyCode.RightArrow)) {
    26.             treeBranch.transform.Translate(new Vector3(0, -branchDownNumb, 0));
    27.             ScoreScript.scoreValue += 1;
    28.             playerFlipped = true;
    29.         }
    30.     }
    The error I get is: "NullReferenceException: Object reference not set to an instance of an object"
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,050
    So first, make sure the TreeBranch instantiation is happening before you try to get the reference in the other script. Easy way to do this is to put the instantiation in Awake.

    Second, after the object is instantiated, check the name in the hierarchy. Is it just TreeBranch or is it TreeBranch(clone)?
    Make sure the name matches up exactly.

    If that still returns null, you can add a unique tag and find it that way, but the name should work.
     
    snoots and TheNightglow like this.
  3. snoots

    snoots

    Joined:
    Mar 14, 2021
    Posts:
    6
    Hey! Thanks for the reply, unfortunatly it still didn't work.
    I added an awake to the tree branch and change it to find "TreeBranch(Clone)" instead.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,050
    Just to tie up a loose end, can you confirm which line the null ref error is occurring at?
     
  5. snoots

    snoots

    Joined:
    Mar 14, 2021
    Posts:
    6
    It occurs on GetKeyDown Input

    Update: I made the GameObject treeBranch public, and manually put the TreeBranch prefab into the variable (using unity's UI)

    It doesn't give me a null error, but the tree branch doesn't move. It stays at the same place it was instantiated.

    Main Script
    Code (CSharp):
    1. private float branchDownNumb;
    2.     public GameObject treeBranch;
    3.     private bool playerFlipped;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         branchDownNumb = 2;
    9.         playerFlipped = false;
    10.     }
    11.  
    12.     private void OnTriggerEnter2D(Collider2D collision) {
    13.         if(collision.name == "TreeBranch(Clone)") {
    14.             SceneManager.LoadScene("GameEndScene");
    15.         }
    16.     }
    17.  
    18.     void lumberJackController() {
    19.         if(treeBranch == null){
    20.             return;
    21.         }
    22.         if(Input.GetKeyDown(KeyCode.LeftArrow)) {
    23.             treeBranch.transform.Translate(new Vector3(0,-branchDownNumb,0));
    24.             ScoreScript.scoreValue += 1;
    25.             playerFlipped = false;
    26.         }
    27.         if(Input.GetKeyDown(KeyCode.RightArrow)) {
    28.             treeBranch.transform.Translate(new Vector3(0, -branchDownNumb, 0));
    29.             ScoreScript.scoreValue += 1;
    30.             playerFlipped = true;
    31.         }
    32.     }
    Tree Branch Spawner
    Code (CSharp):
    1.     public GameObject TreeBranch;
    2.  
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         spawnEnemy();
    7.     }
    8.  
    9.     void spawnEnemy() {
    10.         GameObject newBranch = Instantiate(TreeBranch);
    11.         newBranch.transform.position = new Vector3(-1.5f,10,0);
    12.     }
     
    Last edited: Jun 3, 2021
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,050
    Throw in a debug.log for both the Right and Left Arrow to make sure it is even calling in the first place. Also, make sure your branchDownNumb has a value, and if it does, maybe make it larger if the function is calling but still not moving
     
  7. snoots

    snoots

    Joined:
    Mar 14, 2021
    Posts:
    6
    The Left and Right arrows are working perfectly fine, as for the translate, I changed the branchDown from 2 to 20 and it still doesn't move, not even by 0.001.

    I really appreciate for you trying to help by the way, this is just such a weird bug.
    I looked everywhere on this forum and there are more questions than answers on this issue.
     
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,050
    So here are a couple things you may want to try:

    First, change your Translate parameters to something similar to what is in the documentation: upload_2021-6-4_8-15-30.png

    Also, maybe instead of GetKeyDown, just use GetKey so you can hold the button and have it move. I know you are adding/counting with each press but try to just get it moving first and see if you can have a work around for the rest.
     
unityunity