Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiating prefabs causes errors

Discussion in 'Getting Started' started by romanpinkel, Sep 12, 2019.

  1. romanpinkel

    romanpinkel

    Joined:
    Sep 4, 2019
    Posts:
    20
    Hey,
    today Im really troubled...
    I habe a public prefab which should spawn a red capsule (I have put an prefab into the the inspector..)
    In the update Method Im trying to spawn it but just get an error:

    this is the error message:

    UnassignedReferenceException: The variable myPrefab of JumpScript has not been assigned.
    You probably need to assign the myPrefab variable of the JumpScript script in the inspector.
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) <0x1d6ab5b0 + 0x00062> in <578b5b677e3045f586753155bc1864cb>:0
    UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:202)
    UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:276)
    JumpScript.Update () (at Assets/Scripts/Roman/JumpScript.cs:27)


    Code (CSharp):
    1.  if (Input.GetKeyDown(KeyCode.K))
    2.         {
    3.             Debug.Log("K gedrückt");
    4.             // Instantiate at position (3, 3, 3) and zero rotation.
    5.             Instantiate(myPrefab, new Vector3(3, 3, 3), Quaternion.identity);
    6.         }
     
  2. romanpinkel

    romanpinkel

    Joined:
    Sep 4, 2019
    Posts:
    20
    I solved the problem by just using another script...

    Can I close this one somehow?
     
    Last edited: Sep 12, 2019
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You should have solved it by assigning the myPrefab variable of the JumpScript script in the inspector.

    And no, you can't close a thread. It's OK. Other people may learn from it.
     
    Joe-Censored likes this.