- Home /
Spawning Cube on empty gameobject
Ok I've had this problem for a while now, basically I am trying to spawn a cube prefab on a empty game object.Here is my script.
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour{
public GameObject Cube;
public float spawnTimeDelay = 3f;
public float spawnTimeInGame = 2f;
public Vector3 spawnLocation = new Vector3 (0,2,0);
void Spawn ()
{
GameObject SpawnLocation = (GameObject) Instantiate ( Cube, spawnLocation, Quaternion.identity);
}
// Use this for initialization
void Start ()
{
InvokeRepeating ("Spawn", spawnTimeDelay, spawnTimeInGame);
}
}
Not enough information. Please tell what-it-does as well as what-you-want in a sensible explanation. In what way does this not work?
I created empty game objects to act as spawn locations for the cubes, because I am prototyping a car game where basically cars are supposed to spawn one after another an act as cars. I just want the cubes to move forward to the player after it is spawned. Also It is not working because it refuses to spawn the cube or anything. Hope that helped if , you need more explanation , I will give it to you.
Thanks
How about
GameObject go = GameObject.Instantiate(Cube, spawnLocation, Quaternion.identity) as GameObject;
Since you're not doing anything with the variable, it should be sufficient to just
GameObject.Instantiate(Cube, spawnLocation, Quaternion.identity);
I've changed it , Unity is saying that the variable cube of Spawner hasn't been assigned?
Assign a game object to the Cube variable to this script instance in the inspector. This object should be a prefab.
Answer by melwei · Mar 14, 2015 at 10:48 PM
Try it with:
cube=GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z);
Sorry for posting as an answer , but anyway melwei I did it , all errors are gone now only thing is the cube doesn't appear so you probably fixed my issue , but I might have done something wrong.I'll give you a print screen. The cubes appear in the hierarchy but not in the game when it is being run.
I think you have to place the empty gameobject at another position because the script part I wrote will place a cube at the position of the empty gameobject. If you dont want the cube to spawn at the position of the empty gameobject, you have to put numbers into the Vector3- like this: cube.transform.position = new Vector3(0,2,0)
Here is a part of a script I used.
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(0,-7,0);
It really works. $$anonymous$$aybe you just cant see the cube because it has the same texture like your background.
Bruh , melwei I think you have fixed my problem I a deleting the empty game objects and will just let the cubes spawn at positions in Vetor3 like you said. I am trying to change the color of the cube so I can see it do you know how?
By the way does that script spawn the object repeatedly or just once?
Your answer
Follow this Question
Related Questions
How to make an object spawn in different positions? 2 Answers
spawning game objects 1 Answer
[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers
Spawning won't work 1 Answer
Cannot acces other scipt 1 Answer