Hey all, testing something out. So i created a script with a test variable called Crazy which takes one “Skill” which is a custom class i made. I next created a script which derives from skill and attempted to place it into this slot. However: https://i.gyazo.com/6e161dd50aa50038805297fbed3fb831.gif
It cannot be placed in there, despite being the type of variable that is required. However, if i attach test to the Gameobject and THEN drag it in, it can be placed in: https://i.gyazo.com/4f1c3b20774778e0d3bffabce3e4ba90.gif
is this some sort of limitation or requirement? If so, why?
You have a variable of type skill.
You can assign a reference to instance of the sklill calass / a class derived from it.
The script file you are dragging is not a instance of the class.
If you just like to attach the “test” class to your gamobject it has to derive from monobehavior.
What does the skill class do , does it just store data or does it contain code?
Is there a good reason not to have it as a monobehavior component on the same gamobject and then reference it?
The skill class just contains data, and the prime reason for trying to see if i could put it in without attaching it was because it’s one of those games where you can learn new skills, but you have a limited capacity for how many you can keep, so the idea was that at some times, i can use code to replace certain skills. Something like:
Player.Skills[12] = Resources.Load(“Scripts/Skills/FlameAttack”);
Let me try to explain by way of analogy:
Your slot is expecting a house. When you try to drag the script directly into the slot it’s like trying to put the blueprint for a house into the slot for a house.
When you drag the script below and create the component, it’s like actually building a house from the blueprint. Then you can drag the built house into the slot no problem.
To continue, classes in C# (and other object oriented programming languages) are blueprints for objects. You need to create an instance from the blueprint to do stuff with it.
i figured as much, i just hoped i could do without that, seeing as it means i need an object with every skill present at all times, even in non-battle scenes, in order to prevent a nullreferenceexception.
also, as seen here: https://i.gyazo.com/167732999c1f352f37f11acd225f53f4.png
i set the first skill to be one named “test” on a database object, and later set the UI to update each button on the skill menu with the name of the skill in that slot:
for(int i = 0; i < 5; i++)
{
Text buttontext = Buttons[i].GetComponentInChildren<Text>();
if (Player.skills[i].name != null)
{
Debug.Log(buttontext);
Debug.Log(Player);
buttontext.text = Player.skills[i].name;
}
}
however, the button is updated with the name of the attackdatabase object, not the skill on it. What can be done to remedy this if the skill class cannot be put into the slot on its own?
Maybe look into ScriptableObjects for your skill definitions. They behave more like what you’re looking for.
as in they can be placed into variable slots without an attached gameobject? thats what i want, if i could just figure out something like that i should be able to come up with the rest on my own.
edit: indeed they can, thank you.
1 Like