Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gevatter · May 17, 2011 at 08:43 PM · arrayvariablevalue

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 () {

} }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Maarten · May 17, 2011 at 08:53 PM 0
Share

Do you have some sample code of your database class?

avatar image gevatter · May 17, 2011 at 09:09 PM 0
Share

No problem... I added the whole Database Script.

As I said before, when I use DATABAS$$anonymous$$Test; it works.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gevatter · May 17, 2011 at 09:36 PM 0
Share

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

avatar image DaveA · May 17, 2011 at 11:51 PM 0
Share

If all you want is to store that string on the DATABASE object, then just add it there, see code above

avatar image CHPedersen · May 18, 2011 at 06:57 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to get a value from an array within another script. 1 Answer

Array Problem - Error Code BCE0022 1 Answer

Getting error: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable 1 Answer

Array Problem - Error Code BCE0022 2 Answers

Variable Type for an Instance of any Script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges