Random spawn script
Hi! This is a complicated thing and i am just a beginner at scripting, so please be pacient. As i mentioned in a question before, i am making a party scene in my game, and there is a minibar on it. The minibar has a lot of drinks, and there are 3 cups. I wanted to do a minigame, in wich when you hit the trigger of the cups you enter in a scene in wich you need to guess the right cup and then you will get a trophy.
I already did the thing about making the character go to the scene, and i plan to do the trophy later. My problem is with the minigame itself! I have 3 cups, and i want a sphere to randomly spawn in one of the cups and then be a child of it. But my knowledge about spawning is terrible and i never understood how to get an object to be child of another using scripts. Could someone lend me a hand here? I would be very grateful =]
Answer by b1gry4n · Oct 23, 2016 at 04:07 AM
public Transform[] cups;
public GameObject ballPrefab;
int chosen = Random.Range(0, cups.Length);
GameObject ball = (GameObject)Instantiate(ballPrefab, cups[chosen].position, Quaternion.identity);
ball.transform.parent = cups[chosen];
Your answer
Follow this Question
Related Questions
Making an endless hallway with random objects spawning 0 Answers
Unet Spawn Prefabs When Scenes Changes 0 Answers
Problem spawning prefabs client side, stand alone build. 0 Answers
Characters not attaching to prefab location when spawned? 0 Answers
How to make a prefab spawn on the same spot on a rotating object? 1 Answer