Random Instatiation.

Alright, I did google for something along the lines of this but either I didn't understand or it had other things in the script which confused it for me.

I want to pick a random instiation.

So lets say I have 2 blood splats (They spawn on the ground and destory themselves after a minuite my game is Top down so not particle effect in my art style) and I want a random one to instantiate each time i get hit. I know that one works when I put it inside my hurt script I just want a random one (If you get me, Not very good at explaining). I am unsure on how to do this How would I generate the number between 0-1 and apply it to this.

if (randomnumber == 1)
{
Instantiate(ptrScriptVariable.objBPool, objPlayer.transform.position, Quaternion.identity );
}
else if (randomnumber == 0)
{
Instantiate(ptrScriptVariable.objBPool1, objPlayer.transform.position, Quaternion.identity );
}

Cheers, Aaron Lewis

Have objBPool be an array of GameObjects, and select randomly from the array using Random.Range:

var objectArray = ptrScriptVariable.objBPool;
Instantiate (objectArray[Random.Range(0, objectArray.Length)], objPlayer.transform.position, Quaternion.identity);

I would create an array of the blood splats, and use Random.Range() to determine which to display. Then you can use Destroy() to cleanup the blood splat with a 60 second delay.

var bloodSplatPool = ptrScriptVariable.objBPool;
var tempSplat = Instantiate(bloodSplatPool[Random.Range(0,bloodSplatPool.length),
                            objPlayer.transform.position,
                            Quaternion.identity );
Destroy(tempSplat, 60);

Comment back if you have any questions/queries.

Please excuse the horrible formatting - made to fit on a single screen, without a horizontal scrollbar.


C#:

public Material[] bloodSplatPool;    // array of Materials

function WhateverYouCalledIt()
{
   GameObject tempSplat = Instantiate(
                            bloodSplatPool[Random.Range(0,bloodSplatPool.length),
                            objPlayer.transform.position,
                            Quaternion.identity );
   Destroy(tempSplat, 60);
}