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 attacker5 · Oct 23, 2012 at 04:58 AM · variableclassaccess

How do you use a variable in a line of code?

Hello, Alright, I am having a big problem here and I cannot for the life of me find a solution to it.

I have a Javascript file that contains my classes, as of now there is only one.

 //This script holds all classes and there data
 
 class trackInfo
 {
     var scenename : String;
     var name : String;
     var length : int;
     var bestlaptime : float;
     var type : String;
     var series : String;
 }

I then have another script that holds the instances and there variable values.

 //Track Info Holder
 
 static var track1 = trackInfo();
     track1.scenename = "track1";
     track1.name = "Townsville Practice Day";
     track1.length = 300;
     track1.bestlaptime = 999;
     track1.type = "Hybrid Supercross";
     track1.series = "Queensland ClubCross";
     
 static var track2 = trackInfo();
     track2.scenename = "track2";
     track2.name = "Supercross";
     track2.length = 300;
     track2.bestlaptime = 999;
     track2.type = "Hybrid Supercross";
     track2.series = "Queensland ClubCross";

The issue arises when I try to set one of these value dependent on what scene I am in. So the normal way would be the following

 //On GUI
 
 bestlaptime = TrackInfoHolder.track1.bestlaptime;
 

But rather than have track1 in there, I want it to be dependent on my scene, my scene names are track1, track2, track3 respectively.

So my thought would be something like this

 bestlaptime = TrackInfoHolder.(Application.loadedLevelName).bestlaptime;

Of course I have tried this and it doesn't work. Is there any other way to go about a solution to this.

Thank You for your time! Ramon Brand

Comment
Add comment
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

1 Reply

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

Answer by whydoidoit · Oct 23, 2012 at 05:01 AM

You need to use something else rather than pure static variables. You should consider using a Dictionary where the key is the loaded level name:

    static var tracks = new Dictionary.<string, trackInfo>();

    TrackInfoHolder.tracks[Application.loadedLevelName].bestLapTime = xxxx;


This Unity Gems article has a section showing information on using Lists and Dictionaries in Unity Script.

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 attacker5 · Oct 23, 2012 at 06:21 AM 0
Share

Thanks for this, Just one thing, Im getting a error, "$$anonymous$$ Identifier: Dictionary". This seems odd.

Also, I have done some research and it seems as if dictionaries are "out of date"? Because everything is referring to hashtables. Are they the same thing, just new? Thank You

avatar image Eric5h5 · Oct 23, 2012 at 07:38 AM 0
Share

You have it backwards; Hashtable is generally obsolete; use Dictionary ins$$anonymous$$d. As for the error, import the System.Collections.Generic namespace.

avatar image attacker5 · Oct 23, 2012 at 11:02 AM 0
Share

Awesome, after some work I got it going! Very powerful tool for me, thank you both for the help! :)

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access variable from other class? 1 Answer

Can I access variables of scripts that inherit from abstract classes? 2 Answers

How can I access a function without knowing the script/class name to which it belongs? 1 Answer

Collision object and access to a script variable? 1 Answer

How to access a variable in a script on the same object. 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