- Home /
Instantiate a random prefab at an objects location
I wrote a script to instantiate a prefab in place of a destroyed object and it works just fine, but now I want that same code to randomly choose from one of four prefabs. I would prefer to not define the prefabs in the script itself, and leave it to be defined through public GameObject's, just like it already is. I need the script to stay as universal as possible so it can be used on all destructible objects, only requiring me to replace the prefabs each time it's added to a new object. I'm just drawing a blank, I know it's a simple solution, I just can't think of how to do it for the life of me. Thanks in advance.
using UnityEngine;
using System.Collections;
public class Destructible : MonoBehaviour {
public GameObject debrisPrefab;
public GameObject debrisPrefab1;
public GameObject debrisPrefab2;
public GameObject debrisPrefab3;
public float destroyImpact = 20.0f;
void OnCollisionEnter( Collision collision ) {
if( collision.relativeVelocity.magnitude > destroyImpact ) {
DestroyMe();
}
}
void DestroyMe() {
if(debrisPrefab) {
Instantiate(debrisPrefab, transform.position, transform.rotation);
}
Destroy(gameObject);
}
}
An example of the code use is: This code is attached to a brick wall, which is then destroyed. I then have 4 different "destroyed_brick_wall_number" prefabs I want the engine to choose from when replacing the original "brick_wall" object. I currently have it set to use "destroyed_brick_wall_1" in this example and it works fine; but every destroyed wall looks the same. I want them to look different upon destruction, at least a little bit.
Sorry I am away from my pc with the info you need but if I recall correctly you can store your prefabs in an array or you can also use Random.Range. As soon as I boot up the old pc I'll give you an exact answer in the meantime try looking into those two suggestions.
I used Random.Range to make an enemy switch between player1, player2, and player3 randomly. I'm pretty sure that's what your looking for. Also I'm not entirely sure what you mean by public gameObjects. I assume this is C# talk for defining something through the inspector.
Sorry, but "Sorry I am away from my pc with the info you need but if I recall correctly ..." etc. does not constitute an answer. Please watch the tutorial video linked on the right side.
I see what you mean. Next time I'll just try to code it for him.
It wasn't about coding it for me. You gave a generalized answer to a distinct question. I'd say the 2 snippets of code given by Happy$$anonymous$$oo wasn't "coding it for me" but it got me on the right track, and in the end after tweaking some of my original code it worked fine. I do appreciate the effort, and the fact you even gave a response though.
Answer by HappyMoo · Jan 07, 2014 at 04:51 AM
using System.Collections.Generic;
..
public List<GameObject> debrisPrefabs;
..
GameObject fab = debrisPrefabs[Random.Range(0,debrisPrefabs.Count)]
Instantiate(fab, .....);
Thanks a ton! I had to make sure to reference "GameObject fab =" after the "void Destroy$$anonymous$$e()" but before the script called for "fab" in the "if(fab)" statement. Working like a charm now. I'd vote your answer up, but I don't have enough karma; Thank you again for the help.
Answer by Midwest13 · Jan 07, 2014 at 05:31 AM
You probably want to create an array of GameObjects that you would then use Random.Range to randomly choose one. So something like:
public GameObject[] debrisPrefabs;
With this to randomly instantiate one:
Instantiate(debrisPrefabs(Random.Range(0, debrisPrefabs.Length), transform.position, transform.rotation);
I appreciate you taking the time to respond, I didn't use this at the moment, but I'll keep it as reference later if I run into another problem. Thanks.
Answer by NikoBusiness · Jan 07, 2014 at 04:52 AM
it's simple im not going to write the code for you but im going to tell you how to achieve ur goal,so there are 4 prefabs so make a int randomNum = Random.Range(1,4) then make a switch statement if randomNum == 1 then instantiate prefab 1 if 2 the 2nd and so