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 by0log1c · Feb 21, 2011 at 12:20 AM · variabletypeconverting

Turn Strings[] into vars[]?

Okay, I guess I'll make it sounds way more complicated than it actually is but here it goes:

I have a built-in array of Strings and then want to retrieve and manipulate the according variables on the script. ie:

var myVar1:int;
var myVar2:String;
var myVar3:float;
var varNames:String[] = new String[3];
//I go in Inspector and set varNames to ("myVar1","myVar2","myVar3"), then:
for(i=0;i<varNames.length;i++){
    print(varNames[i] + " = " + **varNames[i].ToVar()**);
}

Of course .ToVar() do not exist, this is what I need to find. While I'm at it: .ToType() is something I could definitly use, too (same pseudo-code).

Once again: I'm trying to access a variable having only it name as a String.

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 Eric5h5 · Feb 21, 2011 at 12:44 AM 2
Share

You almost certainly don't actually want to do that. (Though technically it's possible using reflection or eval().) Generally these sort of situations should use arrays or enums.

avatar image by0log1c · Feb 21, 2011 at 01:03 AM 0
Share

woot! eval(myString) is exactly what I was looking for. $$anonymous$$any thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Feb 21, 2011 at 12:46 AM

There is no "easy" way of doing that and it's not recommended to do such things. In most cases there are better solutions. I don't know what you want to achieve in the end but one way would be to use .NET/MONO Reflection. If you really want to use reflection i would recommend C# and visual studio just because of autocompletion and intellisense.

But as i said that's not the way you should go. Reflection is quite slow and if you don't know what you're doing you can even crash your game. Maybe tell us what exactly you want to achieve and we find the best solution.


Edit
In this example I use 4 scripts (for my predefined property types). You can drag&drop one or more properties to a gameobject and give them a name. I use the component system of unity to search for them. Thanks to the base class "IProperty" you can set the value with get() and set() as string.

// IProperty.js  - should not be added to any gameobject, it's just the base class
class IProperty extends MonoBehaviour
{
    var Name : String;
    function Set(aValue : String) { } ;
    function Get() : String { return ""; };
}
// ****************************************************************************
// IntProperty.js
class IntProperty extends IProperty {
    var value : int;
    function Set(aValue : String) {
        value = int.Parse(aValue);
    }
    function Get() : String {
        return value.ToString();
    }
}
// ****************************************************************************
// FloatProperty.js
class FloatProperty extends IProperty {
    var value : float;
    function Set(aValue : String) {
        value = float.Parse(aValue);
    } 
    function Get() : String {
        return value.ToString();
    }
}
// ****************************************************************************
// StringProperty.js
class StringProperty extends IProperty {
    var value : String;
    function Set(aValue : String) {
        value = aValue;
    } 
    function Get() : String {
        return value;
    }
}

To access the variables:

private var properties : IProperty[] = null;

function GetPropertyByName(aName : String) : IProperty { if (properties == null) properties = GetComponents.<IProperty>(); for (var P : IProperty in properties) { if (P.Name == aName) return P; } return null; }

var health : FloatProperty = null;

function Awake() { health = GetPropertyByName("Health") as FloatProperty; }

function Start() { // fast, direct way health.value = 100.0f; // slower, dynamic way GetPropertyByName("Health").Set("43.3"); }

Well, that's just a concept. The function GetPropertyByName should also be boxed in a strategy-pattern and all scripts that have dynamic variables should implement that interface. Finally i didn't turn IProperty into an interface just because it easier with the "Name" var ;). In general the interface should implement a GetName() function. But as i said, it's just a concept.

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 by0log1c · Feb 26, 2011 at 06:53 AM 0
Share

I've read a little and I'd definitly avoid eval() especially since I run into problems evaluating more complex entry. What I need is a script where $$anonymous$$m members enter aScript,aVariable, and aValue as String. Then the script will access and compare/set defined variable, as part of a system.

I've got everything working with int and float, but the String/var syntax is giving me a headache.

avatar image Bunny83 · Feb 26, 2011 at 12:16 PM 0
Share

What kind of variables? What's the difference between those variables? What's the type of those variables. I've written a "small" solution for that kind of problem. It was quite annoying to use JS but in the end it wasn't too different to C# ;). I will add it to my answer

avatar image by0log1c · Mar 02, 2011 at 12:10 AM 0
Share

It ends up being the most stupid mistake of them all. I had the int variables working all along and couldn't figure what was wrong, after numerous tests....... the variable I was accessing was a reserved keyword therefore throwing an error x( . I'm ashamed, but also thankful to everyone here for their time. Especially Bunny83, I might inspire from your code...

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

C# - passing a Type var to GameObject.AddComponent 1 Answer

AddComponent with parameter variable 2 Answers

How to store class type in variable 2 Answers

Set variable as type "script" 1 Answer

How would I make the GetComponent field a variable? 1 Answer


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