Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question trying to instantiate a prefab, then assign it to a variable (C#)

Discussion in 'Scripting' started by Lenticularic, Jul 15, 2021.

  1. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    so I'm trying to instantiate a prefab "T2bullet", and then assign it to the variable "bullet".

    here's the code snippet:
    Code (CSharp):
    1. if(ShotTier == 3) {
    2.             Instantiate(T2bullet, PreviewPoint.transform.position, Quaternion.identity);
    3.             bullet = GameObject.Find("bullet3").gameObject;
    4.         }
    unity is saying that the variable isn't set to an object even though I set it to the object.
    "NullReferenceException: Object reference not set to an instance of an object"
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    All you need is this:
    Code (CSharp):
    1. bullet = Instantiate(T2bullet, PreviewPoint.transform.position, Quaternion.identity);
    No need to bring an expensive and fragile GameObject.Find() operation into the mix.
     
    Lenticularic likes this.
  3. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    wait, you can do that?

    also, what do you mean by "expensive and fragile"?
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    If you look at the documentation for Instantiate, it says it returns the created GameObject.

    GameObject.Find()
    pretty much loops through every object in your scene, which can get expensive. What he means by fragile, is just a guess, but if you change the name of the object at all or spell something wrong your code will break.
     
    Lenticularic likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Yes of course you can do that. It would be absolutely silly if you could not. An analogy for trying to use GameObject.Find for this is like:

    Imagine you walked up to a gumball machine, you put in a quarter, and let the gumball you just bought fall into a huge pile of gumballs on the floor. Then you spent the next 10 minutes sorting through the pile on the floor searching for the one you just bought.

    You could have just grabbed the gumball as it was dispensed from the machine instead, which is what my example code does.

    As for the "expensive and fragile" comment:

    Expensive: it searches all the objects in the scene for an object with the name you're looking for
    Fragile: If you change the name of the prefab or just misspell the name, it won't work.
     
    Lenticularic likes this.
  6. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Everytime someone use GameObject.Find everyone forget question and focus only on that.In your code you instantiate bullet but on third line you try to assign GameObject bullet to some other gameobject called "bullet3" in your scene so do this :

    Code (CSharp):
    1. public class UnityForum : MonoBehaviour
    2. {
    3.     public GameObject T2bullet;
    4.     Transform PreviewPoint;
    5.     GameObject bullet;
    6.     int ShotTier;
    7.  
    8. void Start()
    9.     {
    10.         if (ShotTier == 3)
    11.         {
    12.             bullet = Instantiate(T2bullet, PreviewPoint.transform.position, Quaternion.identity);
    13.             bullet.transform.tag = "Test";  // You can use anything you need on that instantiated bullet
    14.         }
    15.  
    16.     }
    17. }
    With this code on line 12 you will set things to instantiated bullet and not to some other gameobject.
     
    Last edited: Jul 16, 2021
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    GameObject.Find(string) also requires an exact match, and your bullet was most likely being called "bullet3 (Clone)" after being instantiated.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    That's because the GameObject.Find usage in the OP is central to the problem, for two reasons:
    1. OP wasn't even using the correct name, which would have been "bullet 3(Clone)"
    2. Even if they used the correct name, GameObject.Find is just as likely to find any "bullet 3(Clone)" in the scene, not just the one that was just instantiated.
    Find() is simply not an adequate tool for the task in this case, and that's why we're focusing on it.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756