- Home /
Instantiate object in world C#
I have been looking all over the Unity Forums and Unity Answers for the last two days trying to figure this out. I am trying to spawn a prefab, called cube1, into the world on the start of the scene. Thus far, I have not gotten it to work. My script is very simple.
using UnityEngine;
using System.Collections;
public class RandomPlacement : MonoBehaviour {
public GameObject myCube;
public Vector3 spawnSpot = new Vector3(0,2,0);
void Start() {
GameObject cubeSpawn = (GameObject)Instantiate(myCube, new Vector3(0,2,0), transform.rotation);
}
void Update() {
}
}
I have dragged the prefab onto the "Default References" section for "My Cube" in the inspector on my script. I have also tried using
GameObject cubeSpawn = (GameObject)Instantiate(Resources.Load("Cube1"));
but that has not worked either. Can someone please help? Do I need to have the script and prefab in the same directory? (Under project, the script is in "Scripts" and the prefab is in "Resources").
I do get some errors or warnings having to do with GameObject and whatnot when I try a different method, and I always seem to get the warning "The Variable "cubeSpawn" is assigned but its value is never used". Does that make a difference?
Anyway, even when it does not error, the cube does not spawn.
Answer by Chris D · Jun 29, 2011 at 04:01 PM
I think the problem is that you're trying to instantiate the cube from itself in a scene where it does not yet exist.
If there isn't a GameObject already in the scene to which you can attach this script, nothing will be run. Start() runs when the object becomes active, but you need to have something there to call it.
Try this instead:
place an empty gameobject in the scene
attach your script to it
That should allow it to be called.
That worked! Thanks so much!
Does it apply for all scripts that you must have them attached for something in order to be run?
Well, you have to have something running them in general. Even if it's just an empty gameObject (as in my answer), if code isn't attached to something in the scene or called from something in the scene, unity doesn't have a way of getting to it. Scripts aren't just run once they're created, they have to have specific instances.
I think there might be a way to tell a script to compile/run anyways, but I can't remember it off the top of my head. For now, just assume you have to run code from your scene : )
Answer by Ludiares.du · Jun 29, 2011 at 03:56 PM
Try changing this line:
GameObject cubeSpawn = (GameObject)Instantiate(myCube, new Vector3(0,2,0), transform.rotation);
for:
GameObject cubeSpawn = Instantiate(myCube, spawnSpot, Quaternion.identity);
or simply if you don't want to modify it anymore:
Instantiate(myCube, spawnSpot, Quaternion.identity);
Simply returns error:
"Assets/Standard Assets/Scripts/RandomPlacement.cs(10,28): error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)"
Answer by cornflakers · Jan 16, 2012 at 07:45 PM
using UnityEngine; using System.Collections;
public class onLoad : MonoBehaviour {
public GameObject Character;
public Vector3 spawnLocation = new Vector3(0,2,0);
// Use this for initialization
void Start () {
GameObject SpawnLocation = (GameObject)Instantiate(Character,spawnLocation,Quaternion.identity);
}
// Update is called once per frame
void Update () {
}
}