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 help

Discussion in 'Scripting' started by tim76, Nov 11, 2014.

  1. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    Hey,

    New here so this has probable been ask 100 times, but I an having in issue with Instantiate, can could use some help.

    I created a cube then a prefab which I named "Timtest"
    the folder my prefabs in is called...

    ...Assets/prefab

    I build a collider on nother object so that when player hit the object a script will build the prefab. (or a least I'm trying to build that script) That scripted is called "BuildHed" and is below...


    var Timtest : GameObject;

    function OnTriggerEnter(Other : Collider)
    {
    if (Other.tag == "Player")
    {
    var h : GameObject = Instantiate(Timtest);
    }
    }


    I am getting error on the 7th line,...
    The variable Timetest of Timtest of BuildHed has not been assigned.

    I do not know why I am getting this error,
    Any help would be greatful.

    thanks
     
    Last edited: Nov 11, 2014
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Check if you've assigned the "Timtest" prefab from your Prefabs folder to the "Timtest" variable slot in the Inspector.
     
  3. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    Well, If I understand correctly, you have the variable Timtest, a GameObject, and you are trying to instanciate it.

    The error you are getting means that the variable is empty (null) when you are calling OnTriggerEnter, and it can't instanciate it as a result.

    As said above, you need to assign a GameObject to this variable, be it via the inspector or via the code.
     
  4. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    Duhhhhhh.....I am stupid... such a simple thing to overlook

    The Script I was making just before this one, I was getting the GameObject by code and when I made this one I overlooked that I had drop the GameObject into the Inspector panel....

    Thanks,