I am making a quiz and I want a script to random other scripts. thanks for your time
I think going about it by writing each question as an independent script and grabbing a random script is the wrong way to to solve this problem. My suggestion would be to learn how to create a non-Monobehavior class in Javascript, and create a class that captures, 1) the question, 2) possible answers, and 3) correct answer. Then you create an array of these objects and select from the array to display the answer. Note that implementations I just outlined for a Q and A app has been addressed on Unity Answers a few times, so you can probably search out some starter code.
But for your specific question, here is a solution using a script for each question:
#pragma strict
public var questCount = 3;
public var currQuest : MonoBehaviour = null;
function Start() {
var i = Random.Range(1, questCount+1);
currQuest = gameObject.AddComponent("Quest"+i);
}
function Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
gameObject.Destroy(currQuest);
var i = Random.Range(1, questCount+1);
Debug.Log(i);
currQuest = gameObject.AddComponent("Quest"+i);
}
}
This script expects your question scripts to be named ‘Quest1’, ‘Quest2’, ‘Quest3’… It adds and removes those scripts from the current game object.
The next thing you’ll probably want is to not repeat questions. The question of random selection without repeating has been covered numerous times on Unity Answers. Your solutions is to either put your script names in an array and shuffle the array or to put them in a generic List and remove them as you use them.