- Home /
Accessing array in another script - Error message
I've got one script which defines an array (GUI_Main.js):
var thumbnailTextures : Texture2D[];
And a script (attached to another game object) that tries to access the first element from the thumbnailTextures array (AnotherScript.js):
GameObject.Find("GUI_Main").thumbnailTextures[0] = obj.renderer.material.mainTexture;
And I am getting the error message: Assets/Take Script.js(49,53): BCE0019: 'thumbnailTextures' is not a member of 'UnityEngine.GameObject'.
I think, I didn't really understand the concept of accessing variables in other scripts. Can somebody help me? Thx.
Answer by Maarten · Nov 30, 2010 at 11:47 AM
You are returning a GameObject. You should use GameObject.Find("GUI_Main").GetComponent("ScriptName").thumbnailTextures[0]
Thank you! Works fine. Now i finally understand the concept :-)
Answer by Proclyon · Nov 30, 2010 at 11:47 AM
Most people don't so don't worry , it's something you learn by programming a lot. And a case of aspirin in the start.
The quickest way is to make an object instance of that script
class Foo() { public int x = 3; private int y = 4; }
class Bla() { //Calls default constructor just ignore the whole constructor thing for now Foo myFooObjectInstance = new Foo();
//Allowed
int myBlaInt_X = myFooObjectInstance.x;
//NOT ALLOWED! private is blocking acces
int myBlaInt_Y = myFooObjectInstance.y;
}
An array can be accesed the same way but you should really just store the array in where you need it and acces it there or use a get{}set{} which helps with reading/writing and letting the other script know you are "potentially" messing it up (But if all that's to much information for now just forget it ok)
EDIT:
It was just brought to my attention you may not have any use for C# , I think it's a bit of a waste to just delete this answer but I would like to apoligize for the sloppyness of the answer. I'm afraid I can not do much in JS support but wish you good luck!
oh my , didn't even notice. So used to answering in C#!
Good point. Going to leave it here for posterity and edit and apoligy
No problem! I am sure, the C# code will help other users as well...