How do i acces this array outside the start function?

I’m trying to make a simple multiple choise game inside the GUI. I put a fragment of the code below to try to explain my problem.

See the code below. The array vragenArray is made outside the start function and works perfectly. The StelVraag(); function also made outside and works perfectly(Its also creating ranom numbers).

What I want is to acces one of the AntwoordAr ouside the startfunction. Right now the Debug.Log gives me Null

   function Start () 
     {
          
          random = Random.Range(1,4);
                  
          AntwoordAr[0] = vragenArray[StelVraag(),StelVraag()];
          AntwoordAr[1] = vragenArray[StelVraag(),StelVraag()];
          AntwoordAr[2] = vragenArray[StelVraag(),StelVraag()];
          AntwoordAr[3] = vragenArray[StelVraag(),StelVraag()];
          
          AntwoordAr[random] = vragenArray[random,StelVraag()] + "!";
          
          button1.GetComponentInChildren(UI.Text).text = AntwoordAr[0];
          button2.GetComponentInChildren(UI.Text).text = AntwoordAr[1];
          button3.GetComponentInChildren(UI.Text).text = AntwoordAr[2];
          button4.GetComponentInChildren(UI.Text).text = AntwoordAr[3];
          
              
    	vraag.GetComponentInChildren(UI.Text).text = Questions[random];
    	
     }
     Debug.Log(AntwoordAr[0]);
     //this is what I want to work!

Hope this makes any sence!

Could be reading this wrong but are you creating the vragenArray after Start if so then AntwoordAr will always be empty as vragenArray hasn’t been created yet.

I’d need to see more of the script to be sure but I think that’s what’s happening.

Start() only runs at the start of play.

Hope that helps.

EDIT:

Try moving the lines 72 - 105 up to line 28 and see what the results are.

EDIT 2:

I’m far from being an expert on JS but I got this working. I’m not sure it’s ideal and really I’d rather store the question/answers in a list attached to a separate empty gameobject rather than have them in Start.

Also I don’t think the score is working but hard to say as I’m no language expert either.

#pragma strict
import UnityEngine.UI;


private var number = 0;
private var goedFout;

private var random : int;
private var antwoordA : String; 
private var antwoordB : String; 
private var antwoordC : String; 
private var antwoordD : String; 

static var joppie;

public var punten : Transform;
public var vraag : Transform;
public var button1 : Button;
public var button2 : Button;
public var button3 : Button;
public var button4 : Button;

private var Questions : String[] = ["Drol","Aap","Fiets","Hond","Huis"];
private var ButtonArray : Button[] = [button1,button2,button3,button4];
private var AntwoordAr : String[];
AntwoordAr =[antwoordA,antwoordB,antwoordC,antwoordD];

public var vragenArray : String[,]; 



 function Start () 
 {	 
     //MakeArray();
	vragenArray = new String[11,11];
	
	vragenArray[1,1] = "Aap";
	 vragenArray[1,2] = "Monkey";
	 vragenArray[1,3] = "Affe";
	 vragenArray[1,4] = "Singe";
	 vragenArray[1,5] = "Mono";
	 
	 vragenArray[2,1] = "Fiets";
	 vragenArray[2,2] = "Bicycle";
	 vragenArray[2,3] = "Fahrrad";
	 vragenArray[2,4] = "Vélo";
	 vragenArray[2,5] = "Bicicleta";
	 
	 vragenArray[3,1] = "Hond";
	 vragenArray[3,2] = "Dog";
	 vragenArray[3,3] = "Hund";
	 vragenArray[3,4] = "Chien";
	 vragenArray[3,5] = "Perro";
	 
	 vragenArray[4,1] = "Huis";
	 vragenArray[4,2] = "House";
	 vragenArray[4,3] = "Zuhause";
	 vragenArray[4,4] = "Maison";
	 vragenArray[4,5] = "Casa";
	 
	 vragenArray[5,1] = "Water";
	 vragenArray[5,2] = "Water";
	 vragenArray[5,3] = "Wasser";
	 vragenArray[5,4] = "Eau";
	 vragenArray[5,5] = "agua";
	 
	 ButtonPressed();
	
 }

 
 function ButtonPressed()
 {
 	  goedFout = true;						//goodError
      random = Random.Range(1,4);
      
       Debug.Log(random);
      
      joppie = AntwoordAr[1];
      
      AntwoordAr[0] = vragenArray[StelVraag(),StelVraag()];				//vragenArray = Question Array     AntwoodAr = Answer array - best guess
      AntwoordAr[1] = vragenArray[StelVraag(),StelVraag()];
      AntwoordAr[2] = vragenArray[StelVraag(),StelVraag()];
      AntwoordAr[3] = vragenArray[StelVraag(),StelVraag()];
      
      AntwoordAr[random] = vragenArray[random,StelVraag()] + "!";
      
      button1.GetComponentInChildren(UI.Text).text = AntwoordAr[0];
      button2.GetComponentInChildren(UI.Text).text = AntwoordAr[1];
      button3.GetComponentInChildren(UI.Text).text = AntwoordAr[2];
      button4.GetComponentInChildren(UI.Text).text = AntwoordAr[3];
      
          
	vraag.GetComponentInChildren(UI.Text).text = Questions[random];
 }
 
 
 
function pressButton1() {
	if (Questions[random] == vragenArray[random,1] || Questions[random] == vragenArray[random,2] || Questions[random] == vragenArray[random,3] || Questions[random] == vragenArray[random,4]){
	
	Debug.Log("Goed!"); Score(); ButtonPressed ();
	}
	else{
	Debug.Log("Fout!"); ButtonPressed ();
	}
	
	}
 
 

	
 
 function pressButton2() {

	if (Questions[random] == vragenArray[random,1] || Questions[random] == vragenArray[random,2] || Questions[random] == vragenArray[random,3] || Questions[random] == vragenArray[random,4]){
	
	Debug.Log("Goed!"); Score(); ButtonPressed ();
	}
	else{
	Debug.Log("Fout!"); ButtonPressed ();
	}
	
	}
	
	
function pressButton3() {

	
	if (Questions[random] == vragenArray[random,1] || Questions[random] == vragenArray[random,2] || Questions[random] == vragenArray[random,3] || Questions[random] == vragenArray[random,4]){
	
	Debug.Log("Goed!"); Score(); ButtonPressed ();
	}
	else{
	Debug.Log("Fout!"); ButtonPressed ();
	}
	
	}
	
function pressButton4() {

	if (Questions[random] == vragenArray[random,1] || Questions[random] == vragenArray[random,2] || Questions[random] == vragenArray[random,3] || Questions[random] == vragenArray[random,4]){
	Debug.Log("Goed!"); Score(); ButtonPressed ();
	}
	else{
	Debug.Log("Fout!"); ButtonPressed ();
	}
	
	}
	
	
 

 
function StelVraag() {
    return(Random.Range(1,6));
}

function Score() {
    punten.GetComponentInChildren(UI.Text).text = "Punten: " + (number ++ ) ;
};

I just setup four buttons on 4.6 Canvas and 2 text fields and it updates.

Think I’m at the limit of my abilities though as I really don’t know JS.

Hope it helps.

Here is the script. Hope it makes sence!

#pragma strict
import UnityEngine.UI;

private var number = 0;

private var random : int;
private var antwoordA : String; 
private var antwoordB : String; 
private var antwoordC : String; 
private var antwoordD : String; 

static var joppie;

public var punten : Transform;
public var vraag : Transform;
public var button1 : Button;
public var button2 : Button;
public var button3 : Button;
public var button4 : Button;

private var Questions : String[] = ["Drol","Aap","Fiets","Hond","Huis"];
private var ButtonArray : Button[] = [button1,button2,button3,button4];
private var AntwoordAr : String[];
AntwoordAr =[antwoordA,antwoordB,antwoordC,antwoordD];

 function Start () 
 {
      
      random = Random.Range(1,4);
      joppie = AntwoordAr[1];
      
      AntwoordAr[0] = vragenArray[StelVraag(),StelVraag()];
      AntwoordAr[1] = vragenArray[StelVraag(),StelVraag()];
      AntwoordAr[2] = vragenArray[StelVraag(),StelVraag()];
      AntwoordAr[3] = vragenArray[StelVraag(),StelVraag()];
      
      AntwoordAr[random] = vragenArray[random,StelVraag()] + "!";
      
      button1.GetComponentInChildren(UI.Text).text = AntwoordAr[0];
      button2.GetComponentInChildren(UI.Text).text = AntwoordAr[1];
      button3.GetComponentInChildren(UI.Text).text = AntwoordAr[2];
      button4.GetComponentInChildren(UI.Text).text = AntwoordAr[3];
      
          
	vraag.GetComponentInChildren(UI.Text).text = Questions[random];
	
 }
 Debug.Log(joppie);
 
 
 function pressButton1() {
	if (Questions[random] == vragenArray[random,1] || Questions[random] == vragenArray[random,2] || Questions[random] == vragenArray[random,3] || Questions[random] == vragenArray[random,4]){
	
	Debug.Log("Goed!"); Score(); Start ();
	}
	else{
	Debug.Log("Fout!"); Start ();
	}
	
	}
	

 
function StelVraag() {
    return(Random.Range(1,6));
}

function Score() {
    punten.GetComponentInChildren(UI.Text).text = "Punten: " + (number ++ ) ;
};

 var vragenArray : String[,]; 
 
 
 vragenArray = new String[11,11];
 
 vragenArray[1,1] = "Aap";
 vragenArray[1,2] = "Monkey";
 vragenArray[1,3] = "Affe";
 vragenArray[1,4] = "Singe";
 vragenArray[1,5] = "Mono";
 
 vragenArray[2,1] = "Fiets";
 vragenArray[2,2] = "Bicycle";
 vragenArray[2,3] = "Fahrrad";
 vragenArray[2,4] = "Vélo";
 vragenArray[2,5] = "Bicicleta";
 
 vragenArray[3,1] = "Hond";
 vragenArray[3,2] = "Dog";
 vragenArray[3,3] = "Hund";
 vragenArray[3,4] = "Chien";
 vragenArray[3,5] = "Perro";
 
 vragenArray[4,1] = "Huis";
 vragenArray[4,2] = "House";
 vragenArray[4,3] = "Zuhause";
 vragenArray[4,4] = "Maison";
 vragenArray[4,5] = "Casa";
 
 vragenArray[5,1] = "Water";
 vragenArray[5,2] = "Water";
 vragenArray[5,3] = "Wasser";
 vragenArray[5,4] = "Eau";
 vragenArray[5,5] = "agua";