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

Instantiate Prefab (Striaght Up) Isn't working.

Discussion in 'Scripting' started by SirTwonJohnson, Mar 12, 2015.

  1. SirTwonJohnson

    SirTwonJohnson

    Joined:
    Mar 12, 2015
    Posts:
    5
    I have a script that doesn't destroy on load and "follows" the player around through levels.

    This literally does not work, I've been looking everywhere for a solution :(
    //It's in c#

    pubic GameObject test1;

    //It's in some function and I know its getting there because I have an output statement the line before it
    Instantiate(test1, new Vector3(0,0,0), Quaternion.identity);

    This just does not work. No clone appears in the hierarchy at all.
    I made sure to link the prefab to test1 in the inspector before hand

    I even tried:

    public GameObject test1;
    public GameObject otherObj; //I left it as public to see the value in the inspector at runtime

    otherObj = (GameObject)Instantiate(test1, new Vector3(0,0,0), Quaternion.identity);

    That didn't work either. No clone in the hierarchy, although it shows (in the inspector) that otherObj hold the test1 GameObject.

    Any help is appreciated or any kind of work around, I'm literally at the point of spawning a primitive and adding all the components I need by hand because I've been stuck on this for so long.

    Thanks in advance!
     
  2. RedFireBlitz

    RedFireBlitz

    Joined:
    Feb 18, 2015
    Posts:
    3
    Hi am also a beginner in Unity but maybe i can help you.

    this is how i instantiate my Bullet, so this code is attached to my Player.

    i have a variable:

    Code (CSharp):
    1. public GameObject prefabProjectileA;
    and in the Update:

    Code (CSharp):
    1.  
    2. if(Input.GetMouseButtonDown(0))
    3. {
    4. GameObject projectileA = Instantiate(prefabProjectileA) as GameObject;
    5. projectileA.transform.position = transform.position + Camera.main.transform.forward * 2;
    6. }
    the projectile spawns right infront of me when i click the left mouse button.

    dont forget that you have to drag the prefab into the inspector into the variable slot for prefabProjectileA
     
  3. SirTwonJohnson

    SirTwonJohnson

    Joined:
    Mar 12, 2015
    Posts:
    5
    Thanks for the reply and trying to help, however, the problem may not lie in the code (exactly). I do the same exact thing as you minus moving the position and it just doesn't spawn for me.
     
  4. SirTwonJohnson

    SirTwonJohnson

    Joined:
    Mar 12, 2015
    Posts:
    5
    You know, sometimes, I.. I just don't know anymore man.
    Just for shi**s and giggles I used your code (changed the variable names and left out the transform) and it worked.

    How is it possible that:
    Code (CSharp):
    1. GameObject tmp = Instantiate(prefab) as GameObject; //can work
    2. tmp = (GameObject) Instantiate(prefab, new Vector3(0,0,0), Quaternion.identity); //Does not work
    3. //Maybe unity hates casting in the front?
    4. //or even:
    5. Instantiate(prefab, new Vector3(0,0,0), Quaternion.identity); // Does not work
    I don't know, but thanks...
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    this should work...
    Code (csharp):
    1. var thing = (GameObject)Instantiate(prefab);
    or this..
    Code (csharp):
    1. var thing = Instantiate(prefab) as GameObject;
    typically if you know the type then dont use var, to avoid issues.
    Code (csharp):
    1. GameObject thing = Instantiate(prefab);
    1. is not necessary to define the type twice.
    2. variable is not defined... you would say "var tmp =" instead.
    5. no reference is defined to store the cloned instantiation here, so it cant do anything.

    didn't test anything, sorry if it requires tweaks. It should be correct, though.
     
  6. SirTwonJohnson

    SirTwonJohnson

    Joined:
    Mar 12, 2015
    Posts:
    5
    1. Isn't it that you're telling it tmp is a GameObject and to assign the prefab as a GameObject.
    I'm not saying: GameObject GameObject tmp;
    2. The variable tmp is defined elsewhere as a GameObject(although not mentioned) .
    It's an example of the format
    5. Isn't it that it just puts it into the scene with the name "prefab(clone)" it doesn't need to be assigned to anything. Think if you were using this with a projectile weapon or something. You don't care about referencing the object once it's spawned, you should have a script attached to the prefab that handles everything for you.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's worth noting that Unity 5 has a generic overload of Instantiate so the casting becomes obsolete.

    Code (csharp):
    1.  
    2. GameObject tmp = Instantiate<GameObject>(prefab);
    3.  
     
  8. Dariyoh

    Dariyoh

    Joined:
    Nov 30, 2013
    Posts:
    2
    All three of those should work just fine, so they should show up as a clone in the hierarchy (as long as you did not do anything like "go.hideFlags = HideFlags.HideInHierarchy"). Which means that there is something else going on.

    You mentioned that your script uses DontDestroyOnLoad, are you maybe trying to enforce a singleton on the object that you want to instantiate, and thus Destroy it as soon as it spawns? I can't think of anything else without the full context otherwise, maybe post the full script if you're still having trouble?
     
    Last edited: Mar 12, 2015
  9. SirTwonJohnson

    SirTwonJohnson

    Joined:
    Mar 12, 2015
    Posts:
    5
    Thanks for the note, I don't have Unity 5 quite yet :(

    Thanks for the reply, yeah, I'm not sure, the two just weren't working. I have it working now with the first way listed earlier.
    Its an empty game object that holds some controller scripts (the singleton class destroyer, seek and destroy! get it.. like a naval ship) but it is not the object I'm trying to instantiate. Also I am not using go.hideFlags or anything like that.

    Thanks!
     
  10. Pranav_Redstoneinvente

    Pranav_Redstoneinvente

    Joined:
    Apr 13, 2018
    Posts:
    121
    For some reason Instantiate seems to work in the Update method, when i try to run it in another method that ive made, it simply reacts as if there was an error but throws nothing when inside a Catch.
    Weird
     
  11. DaemonRai

    DaemonRai

    Joined:
    Oct 19, 2021
    Posts:
    1
    Probably a dumb observation on my part, but if this is literally what your code was, it should be public and not pubic.