- Home /
Is it possible to set variables for classes?
I'm basically making a single Game Controller script for many levels.The references of each level is stored in different classes like level 1,level 2 etc.So I need to refer these classes in my controller script .How can set a public variable that denote these classes so i can put that variable in the code
`"variable" reference = GetComponent<"variable">();
@JoenigX3 explained how you can set a variable of type "FirstLeveLScript" and it's a good answer but I think since you're dealing with multiple levels you should look into inheritance since it can handle your question the best way.
FirstLevelScript lvl1 = GetComponent<FirstLevelScript>();
SecondLevelScript lvl2 = GetComponent<SecondLevelScript >();
ThirdLevelScript lvl3 = GetComponent<ThirdLevelScript>();
You will want to do this:
LevelScript lvl = GetComponent<FirstLevelScript>();
lvl = GetComponent<SecondLevelScript>();
lvl = GetComponent<ThirdLevelScript>();
And your class will look like this:
public class LevelScript{
...
}
public class FirstLevelScript : LevelScript{
...
}
public class SecondLevelScript : LevelScript{
...
}
Is this what you're asking?
@jmgek In my game I'm making separate level scripts for each level and i want a single game controller script. which take in reference from current level script which is one from many.I set currentlevel in player prefs so controller can understand which level.Basically I want to put the same controller script for every level and this controller script invoke values from the level script. The problem is where I put reference in the controller for the current "level script".I created level scripts like Level 1,Level 2,Level 3,etc,.... As i put the same controller in every scene the refference in that script should change for example: If it's level 1 it'll be Level1 currentlevel=GetComponent(); and as I put the script in level2 it should change to Level2 currentlevel=GetComponent(); and so on. Currently Im making separate controller scripts by copy pasting the same script but changing the above code. Hope u can help me thanks in advance :)
I am really sorry it's very confusing as to what you're trying to accomplish from that response. What I understand is you want to set one property of type 'Level' in your character controller.
class character
{
LevelScript levelScript;
void Start()
{
if(isFirstLevel)
levelScript = GetComponent<FirstLevelScript>();
if(isSecondLevel)
levelScript= GetComponent<SecondLevelScript>();
if(isThirdLevel)
levelScript= GetComponent<ThirdLevelScript>();
}
}
If you re read what I posted above you can easily assign any level script to 'levelScript' using Class inheritance. As long as each level class inherits off a base level class.
Answer by KoenigX3 · Jan 12, 2017 at 05:40 PM
Use the name of the class as the type of the variable. After you have compiled your Game Controller class (which is, for example the LevelScript):
FirstLevelScript lvl1 = GetComponent<FirstLevelScript>();
This 'lvl1' variable will return the script named FirstLevelScript on the GameObject.
Thanks man, but it's not what I actually need .It's like a Single variable like "CurrentLevel" in the code where Class name should be.So it automatically refers to that level.
`"CurrentLevel" something = GetComponent<"CurrentLevel">();
Sorry for jumping on $$anonymous$$oenigX3 answer but you're not making a lot of sense. Above I posted how you can create a variable that would hold the classes and you could just reference the one you needed. You would set the level that you're currently on.
FirstLevelScript lvl1 = ...;
lvl = ...;
You can't "Automatically" refer to a class. Could you please explain a little better.