- Home /
Question by
DimitriUK · Jul 10, 2015 at 09:46 PM ·
c#gameobjectrandom.rangelength
Random.Range, GameObjects and Length
Hello, I just wanted to know how I can make it so that my script will pick a random wall and then AddComponent a script to it.
using UnityEngine;
using System.Collections;
public class CSpawner : MonoBehaviour {
GameObject[] Walls;
public int index;
// Use this for initialization
void Start () {
Walls = GameObject.FindGameObjectsWithTag("DestroyableWall");
Random.Range (0, Walls.Length);
// I want it to find a random wall, then Add a Component to that randomly selected wall.
}
// Update is called once per frame
void Update () {
}
}
Comment
Best Answer
Answer by wesleywh · Jul 10, 2015 at 09:48 PM
Try out the following:
using UnityEngine; using System.Collections;
public class CSpawner : MonoBehaviour {
GameObject[] Walls;
public int index;
// Use this for initialization
void Start () {
Walls = GameObject.FindGameObjectsWithTag("DestroyableWall");
// I want it to find a random wall, then Add a Component to that randomly selected wall.
Walls[Random.Range (0, Walls.Length)].AddComponent<BlaBla>();
}
}
Ah! I made such a rookie mistake there! I was thinking.. It can't just be Random.Range, etc. Then you wrote that and I was thinking that's what I was trying to wrap around my brain..
Thanks so much Wesleywh!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Random numbers generation on Android devices 1 Answer
Image for a quarter of a second 1 Answer
I have an error, script should check for null or not destroy game objects 2 Answers