- Home /
Variable value as code?!
I want to load an Array from my DATABASE Script. The Array names are the same as the gameobject names but instead of using the VAR "ObjectName" Unity tries to find a Array with the name "ObjectName" inside the DATABASE Script. When I use DATABASE.test; it works.
I hope someone can help me with this =)
private void ObjectWindow(int id) {
ObjectName=Click2Move.hit_objectID;
ObjectLoadArray = DATABASE.ObjectName; //<--
GUI.Label(new Rect(30,30,100,100),"TEST: "+ObjectLoadArray[0]);
}
DATABASE Script:
using UnityEngine; using System.Collections; public class DATABASE : MonoBehaviour{ public static int[] Test = new int [2] {1,2}; void Start () { } void Update () {
} }
No problem... I added the whole Database Script.
As I said before, when I use DATABAS$$anonymous$$Test; it works.
Answer by DaveA · May 17, 2011 at 09:16 PM
DATABASE.ObjectName means that you have a variable named "ObjectName" in you DATABASE class, which you don't. Perhaps you mean DATABASE.Test[ObjectName] assuming ObjectName is an integer 0 or 1 ??
I'm betting what you really want is a Hashtable (google msdn hashtable)
or just this, if all you want is to store that name in the DATABASE object?
public class DATABASE : MonoBehaviour{
public static string ObjectName;
public static int[] Test = new int [2] {1,2};
void Start () {
but from you sample, it looks like you want DATABASE to do something with it, in which case it would be a function:
public static int[] ObjectName (string oname)
{
get and return your array data
}
the VAR ObjectName stores the actual name of the gameobject.
So I want to use the value of ObjectName "Test" and put it behind "DATABAS$$anonymous$$" so that I have a very simple script to refer stored information to every object.
so that everytime when I click on an object a GUI Window pops up which showes the name of the Object and the values of the array.
I have 20 different objects and every one works fine, except for the arrays.
I hate Hashtables, so I would prefer a simpler solution =P
If all you want is to store that string on the DATABASE object, then just add it there, see code above
As a side comment, it sounds to me like you want to inject code at runtime, that is, you want the string stored in ObjectName to replace ObjectName when you reference the variables in DATABAS$$anonymous$$ Only, you can't do that. That's the wrong way to think about program$$anonymous$$g - Variable identifiers such as ObjectName and Test are a compile-time concept, they don't exist at runtime. The program doesn't identify variables by some string-name once the program runs, that's only for us, when we code. A program cannot insert some other code while it runs, that would require it to recompile dynamically.