Simple Code Not Woking
This is just declaring variables, very basic stuff...
When I write this code
public var word = new GameObject[wordLength];
I get these errors:
expecting '}', found 'public'
expecting EOF, found '}'
Can someone explain why this doesn't work? And possibly give an alternative that does? Thanks in advance!
It looks like you probably have an extra curly brace somewhere. Post the rest of your code if you want more precise help.
here is my function... sorry about it being so complicated
function BuildStage(){
public var word = new GameObject[wordLength]; //the array of panels
var matID: int; //tells what material to get based on what letter
var bluOffset: int = 64; //offset value to get blue letters from matID
var redOffset: int = 38; //offset to get red letters from matID
//Instantiate Stage object
Instantiate(stage, gameObject.transform.position, Quaternion.identity);
//and call its Build function
stage.GetComponent(StageScript).Build(wordLength);
//this runs thru and sets both correct materials for every panel
for(var i = 0; i < wordLength; i++){
word[i] = Instantiate(panel, Vector3(i * 1.5, 0, 0), Quaternion.identity);
word[i].GetComponent(PanelScript).myID = i;
word[i].GetComponent(PanelScript).arrSize = wordLength;
//setting prePanel and posPanel
if(i > 0){
//set this guy's prePanel...
word[i].GetComponent(PanelScript).prePanel = word[i - 1];
//and the last guy's posPanel
word[i-1].GetComponent(PanelScript).posPanel = word[i];
}
matID = wordOne[i];
word[i].GetComponent(PanelScript).matOne = matBank[matID - bluOffset];
matID = wordTwo[i];
word[i].GetComponent(PanelScript).matTwo = matBank[matID - redOffset];
}
}
Answer by LazyElephant · Mar 25, 2016 at 12:25 AM
Delete the "public" from public var word = new GameObject[wordLength];
Public is an access modifier used in class definitions and with class members/methods. You don't use it when declaring variables inside functions.
Also, I don't know if it's necessary since I always use c#, but it looks like the definition for the array should be var word : GameObject[] = new GameObject[wordLength];
Thanks for the help! I'm not sure if var word : GameObject[] = new GameObject[wordLength];
is the correct way to declare, but var word = new GameObject[wordLength];
certainly worked for me.
Now I just need to find out how to make the word variable accessible from another function in the script, because when I try that I get the error: 'word' is not a member of $$anonymous$$anagerScript.
The reason I need to declare the array within the function is because wordLength is null until just before the function is called. Declaring the var globally and then setting the array size later could be a work around, but I'm not sure how to do that either. Any suggestions?
//outside the function
var word : GameObject[];
function BuildStage(){
//inside the function
word = new GameObject[wordLength];
//...
}
Also, if the answer is correct, don't forget to mark it correct by clicking the checkmark.