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.

intantiate frustrations :-/

Discussion in 'Scripting' started by dacloo, Feb 1, 2006.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    below: this is inside Update(); I am trying to add spheres as soon as someone presses a key,
    at the location of the object (the "player")

    Snippet:
    Code (csharp):
    1.  
    2. // add bullet!
    3. var sph = GameObject.FindGameObjectWithTag ("sphere_prefab");
    4. Instantiate (sph, transform.position, transform.rotation);
    5. print(transform.rotation);
    6.  
    1. the print line results in the correct rotation value
    2. the prefab is found.

    Error: nullReferenceException for the Instantiate line.
    Why? I really can't see the problem here.

    Entering "transform.rotation" is the same as Quaternation.identity, right?
    Is this Quaterstuff part of transform (subclass)?
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    When you say 'the prefab is found', do you mean you've checked that .FindGameObjectWithTag() is returning a valid reference, or do you mean that you've looked in the project and the prefab is there?

    If it's the latter, try adding...

    Code (csharp):
    1. if(kubus == null)
    2.     Debug.Log("kubus prefab not found");
    ...after the .FindGameObjectWithTag() line.

    I find it a bit odd that you're using .FindGameObjectWithTag() for this purpose anyway. If it were me, I'd add 'var kubusPrefab : GameObject;' at the top of the script and drag the prefab into the appropriate slot on the object in the inspector. Having done that, you'd be able to just do 'Instantiate(kubusPrefab, ...);' instead of having to look up the prefab with .FindGameObjectWithTag() every time you fire.

    Quaternions are rotations, so transform.rotation is a quaternion. Quaternion.identity is a special value which means 'the default rotation' or 'no change in rotation', depending on what you're doing with it.
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Thanks Neil! You are right that I am using a weird method (finding the other object), but I really have to get used to the wonderful idea that the editor "sees" my variables and makes it possible to throw objects in those variables.