- Home /
Question by
Ekta-Mehta-D · Mar 13, 2013 at 09:46 AM ·
instantiateprefabrandom
Prefab Instantiate
I have written simple code to instantiate prefab at runtime.
I have array of prefab. Using Random.Range , generating random number to instantiate prefab. But prefab does not change though different random number is generated.
Code:
Public var bubblePrefabs : GameObject[];
private var futureBubble : GameObject;
function Start () {
futureBubble = GameObject.Find("Sphere(FutureBubble)"); // Next bubble to be shoot
if(futureBubble != null)
{
CreateRandomPrefab(futureBubble.transform.position);
}
}
function Update()
{
var spawnPoint : GameObject = GameObject.Find("Bullet"); // Current Bullet Bubble to be shoot
if(Input.GetMouseButtonDown(0))
{
CreateRandomPrefab(futureBubble.transform.position);
if(spawnPoint != null)
{
Instantiate(bulletSphere, spawnPoint.transform.position,spawnPoint.transform.rotation);
}
}
}
function CreateRandomPrefab(position : Vector3)
{
random = Random.Range(0 , bubblePrefabs.Length);
Debug.Log("Random :" + random);
var bubble : GameObject = Instantiate(bubblePrefabs[random]);
bubble.transform.position = position;
}
But it change color only two times . I want to instantiate every time i click mouse button. So what is the problem? Please help me..
Thanks for ur support and help in advance..
Comment
Your answer
Follow this Question
Related Questions
How to access property of a prefab before Instantiating. 1 Answer
Instantiate issues 1 Answer
,Instantiating Prefab in C# 0 Answers
Randomly pick then create prefab 2 Answers
Destroying Objects. 1 Answer