random array instance

Hello

I want to do a quiz that changes after a wrong answer. Is about a question about a geometric object of the game with 3 buttons. I can load the geometry that i put array of the component but now i want to load it random.

This is my script.

using UnityEngine;

using System.Collections;

public class desafio1 : MonoBehaviour {

	public GUIStyle certa;
	public GUIStyle errada1;
	public GUIStyle errada2;
	public GUIStyle rotulo;
	public GameObject[] predios;
	public GameObject loadPredios;
	string rotulo_erro;
	Random randomNumber = new Random();

	
	// Use this for initialization
	void Start () {
		loadPredios = Instantiate(predios[0],transform.position,Quaternion.identity)as GameObject;		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
		void OnGUI() 
  	{
	GUI.Label(new Rect(0,100,800,50),rotulo_erro,rotulo);
	if(GUI.Button(new Rect(300, 50, 56,56), "", certa )){
				Application.LoadLevel("level2");
		}	
	if(GUI.Button(new Rect(380, 50, 56,56), "", errada1 )){
				rotulo_erro = "OPS";
				Destroy(loadPredios);
				
		}		
	if (GUI.Button(new Rect(460, 50, 56,56), "", errada2 )){
				rotulo_erro = "WRONG";
				Destroy(loadPredios);
		loadPredios = Instantiate(predios[0],transform.position,Quaternion.identity)as GameObject;	
		}	
		      
	}
}

2 Answers

2

If you are trying to use System.Random:

int rnd = randomNumber.Next(predios.Length);
loadPredios = Instantiate(predios[rnd],transform.position,Quaternion.identity)as GameObject;  

But you look to be missing a using System; in this case.

If you mean to use Unity Random, remove your randomNumber declaration and use:

int rnd = Random.Range(0,predios.Length);

If just want to select a building among the array elements, you can use Random.Range(0, array.Length) as the index - it returns a number from 0 to array.Length-1:

using UnityEngine;
using System.Collections;

public class desafio1 : MonoBehaviour {
    public GUIStyle certa;
    public GUIStyle errada1;
    public GUIStyle errada2;
    public GUIStyle rotulo;
    public GameObject[] predios;
    public GameObject loadPredios;
    string rotulo_erro;
    Random randomNumber = new Random();
    
    // Use this for initialization
    void Start () {
        loadPredios = Instantiate(predios[Random.Range(0, predios.Length)], transform.position,Quaternion.identity)as GameObject;      
    }
    
    // Update is called once per frame
    void Update () {
    }

    void OnGUI(){
        GUI.Label(new Rect(0,100,800,50),rotulo_erro,rotulo);
        if(GUI.Button(new Rect(300, 50, 56,56), "", certa )){
            Application.LoadLevel("level2");
        }   
        if(GUI.Button(new Rect(380, 50, 56,56), "", errada1 )){
            rotulo_erro = "OPS";
            Destroy(loadPredios);
        }       
        if (GUI.Button(new Rect(460, 50, 56,56), "", errada2 )){
            rotulo_erro = "WRONG";
            Destroy(loadPredios);
            loadPredios = Instantiate(predios[Random.Range(0, predios.Length)],transform.position,Quaternion.identity)as GameObject;  
        }   
    }
}