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

Instantiate prefab issue

Discussion in 'Scripting' started by cryptoboris, Mar 19, 2018.

  1. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Hi, I've just started to play with Unity 3D engine and I'am stuck with `Instantiate` function.
    I'am using Javascript in my project.
    Reading the docs doesn't help me, I can't find the solution yet.

    I just wat to clone randomly (instantiate) custom object in my scene in some area at random time intervals.
    I.e. I have simple cube in my scene with a permanent movement in the one direction. And I wat to apply this `cloning` script to this object.

    This doc is useless because this example is insufficient so that I have problems with this...


    // JavaScript
    var brick : Transform; // wuuut?
    function Start () {
    for (var y = 0; y < 5; y++) {
    for (var x = 0; x < 5; x++) {
    Instantiate(brick, Vector3 (x, y, 0), Quaternion.identity); // rly?
    }
    }
    }


    As a result I have this:

    "UnassignedReferenceException: The variable brick of rotate has not been assigned.
    You probably need to assign the brick variable of the rotate script in the inspector."

    So, there is many code samples for `Instantiate` function, but all of them are insufficient, suprisingly there is no exact description how to apply this damn function to the specific game object.

    Help me please, thanks!
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    It sounds like the editor is telling you to assign the prefab "brick" in the inspector.
     
    Vryken likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    First I'll say Unity dropped support for Unityscript(also sometimes called Javascript).

    Dragging and dropping through the inspector is a common thing you'll be doing in Unity. Once your script is attached to a GameObject in the scene, you should have access to some variables that are in the script. In this case "brick" which you should be able to drag something into it which it then uses that to Instantiate clones of it. If you don't drag and drop, you'll get that error instead, which is similar to a null error, as the variable has no value.

    The example they give in their doc does something similar, except they assign the value through code vs having a variable that you drag and drop into.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're just starting with Unity, you should immediately drop UnityScript/JavaScript in favor of C#, as UnityScript is deprecated and being removed from the engine. Also, virtually all help and examples are now in C# in the forums and other locations, so it will be far easier to get help.
     
    Homicide likes this.
  5. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Many thanks, guys!
    I thought javascript would be the best choice for me as a web developer, unfortunately I was wrong :(
    (so why JS examples are still in Unity docs? seems it still works with unity..)
    Ok, let me try this in C# :)
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Unity supports what is there and older versions of Unity still use it, but the newer versions don't allow you to easily add new scripts for it. And eventually they will probably remove it from the documents. But like Boo, it doesn't mean it isn't there, just expect it will be harder to get help with it in the future and none of the new stuff will have examples for it.
     
  7. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Unity functions in C# and relation between engine objects are pretty overwhelming...
    Now I'am trying to instantiate a simple cube but can't get how to aply it to the object.
    This tutorial doesn't help at all... the key moment is omitted as if spesially... Why they don't show where and how to specify prefab object for this script properly? Just `public Rigidbody rocketPrefab;` and that's it...cool.

    So, now I have this sketch:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class usingInstantiate : MonoBehaviour
    5. {
    6.   public Rigidbody objPrefab;
    7.  
    8.   void Start()
    9.   {
    10.     Rigidbody objInstance;
    11.     objInstance = Instantiate(objPrefab, Vector3 (0, 0, 0), Quaternion.identity) as Rigidbody;
    12.   }
    13. }
    What should I do with `objPrefab` now?
     
    Last edited by a moderator: Mar 27, 2018
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I didn't watch the video, but you take a prefab and drag n drop it in the inspector 's variable slot for 'objPrefab'.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Using the inspector is a common thing, so the person assumes you know how to use it.
    Create a prefab that has a Rigidbody on it. Add your script above to a gameobject in the scene. Drag and drop your prefab into the variable slot.

    Honestly, this isn't a c# issue. It's a Unity design where you can populate variables through the inspector.
     
  10. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Now I have a problem with SetActive function.
    If I turn on game object for instantiation script works well... but it doesn't work if I turn off this game object in the scene.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Instantiation : MonoBehaviour {
    5.  
    6.   private GameObject forClone;
    7.   private GameObject cloned;
    8.  
    9.   void Start() {
    10.     forClone = GameObject.Find("cloneCube");
    11.     forClone.SetActive(true);
    12.     cloned = Instantiate(forClone, new Vector3(2, 0, 0), Quaternion.identity);
    13.     cloned.SetActive(true);
    14.     forClone.SetActive(false);
    15.   }
    16. }[/ICODE]
    17.  
    18. I guess `[B]GameObject.Find("cloneCube");[/B]` doesn't work if this object inactive in the scene. How to fix this?
     
    Last edited by a moderator: Mar 27, 2018
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's correct. It doesn't work when the game object is inactive. You can reference it directly in your script, using a public variable or a SerializeField attribute on a non-public one.
     
  12. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Do you mean to change this?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Instantiation : MonoBehaviour {
    5.  
    6.   public GameObject mobClone; // <=
    7.   public GameObject cloned; // <=
    8.   ...
    9.  
     
    Last edited by a moderator: Mar 27, 2018
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    well, the one that was previously referenced via 'Find', at least :)
     
  14. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    So, how to reference it directly, not via `Find` function?
    Unfortunately google searching doesn't help me, no examples for gameobject reference api...
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just like in your example above.. public or SerializeField so you can drag n drop it in the inspector. :)
     
  16. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    No, my example doesn't work in case of inactive gameobject at start...
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No, no.. Just change the object you wanted to find to 'public' and drag n drop it in the inspector. :)

    (When I said like your example, I meant how you had 2 public variables in a recent post.. not the other one**).
     
  18. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Still can't get the idea...(
    I've already changed `private GameObject mobClone` to `public GameObject mobClone` and dropped it in the inspector.
     
  19. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    If mobClone is public and you dropped your "cloneCube" gameobject into it in the inspector, you can instantiate mobClone to create a clone of the cloneCube.

    Make sure you aren't still doing a GameObject.FInd and trying to assign another value to mobClone as this will change it's value back to null.
     
  20. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Sorry, I'am too dumb.. C# syntax with Unity inspector look like a shell game for me as beginner.
    Despite everything this tutorial helped me a lot!
    Now I have to find out how to write endless loop with random time intervals in C# :)
     
  21. cryptoboris

    cryptoboris

    Joined:
    Mar 19, 2018
    Posts:
    12
    Done...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class inst : MonoBehaviour {
    5.  
    6.   public GameObject prefabObject = null;
    7.   public float nextActionTime = 0f;
    8.  
    9.   void Start () {
    10.  
    11.   }
    12.  
    13.   void Update () {
    14.     if (Time.time > nextActionTime ) {
    15.       nextActionTime += Random.Range(0.1f, 5.0f);
    16.       // execute block of code here
    17.       Instantiate(prefabObject,
    18.                   transform.position,
    19.                   Quaternion.identity);
    20.     }
    21.   }
    22. }
    23.  
     
    Last edited by a moderator: Mar 27, 2018