- Home /
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
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.
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
You have it backwards; Hashtable is generally obsolete; use Dictionary ins$$anonymous$$d. As for the error, import the System.Collections.Generic namespace.
Awesome, after some work I got it going! Very powerful tool for me, thank you both for the help! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613082749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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