- Home /
GetComponent(string) with unknown ScriptName
Hi,
I got a little Problem with my GetComponent in my delegate... I got an Array full with GameObjects, every GameObject has a Script with the Name: GameObject.name + "Points" Now i made a Method that Sorts my List by checking the value of AtkSpeed inside the Scripts:
public void SortTargetsByAtks(List<GameObject> unitList){
unitList.Sort(delegate(GameObject t1, GameObject t2){
return((t1.GetComponent(t1.name +"Points").stats.AtkSpeed.CompareTo(t2.GetComponent(t2.name + "Points").stats.AtkSpeed)));
});
}
This is the Error: error CS1061: Type `UnityEngine.Component' does not contain a definition for `stats' and no extension method `stats' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
I know i can use GetComponent using
GetComponent<ScriptName>().stats.AtkSpeed
, but the Problem is i don't know the ScriptName... I hope someone can help me, but i Thank you already for just reading my Problem =D
What if you tried a dictionary string/type and then you can easily use a string to find the corresponding class.
This sounds realy nice, i don't know exactly what a dictionary is, but i think this will help my :D If someone has a better Solution, it would be great, because with a Dictionary, i have to make a huge Dictionary....
Answer by edthethird · Dec 02, 2012 at 08:41 PM
Try using an abstract class to accomplish this.
Step 1: Create a new interface:
public abstract class HasStats: MonoBehaviour {
Stat getStats();
}
Step 2: and then go through all your actual scripts (value of `scriptname`) and make them look like this:
public class scriptname : HasStats
Step 3:
GetComponent<HasStats>().getStats().AtkSpeed
and that will do it.
if you want to make this more maintainable in the future you can take further advantage of Abstract classes. I can elaborate if people are interested. Anyways, the above should work for you, good luck!
I thank you very much, i think i don't really understood the meaning of interfaces/abstract classes...
You do not need abstract class or interface here. Those are for another purpose than GetComponent. All you need to do is attach to all your objects the script with one name. The compiler will create an instance of that script for each monster. Each script will have its own set of variables distinctly kept in memory (except for static but let's not go there). Interfaces are simply an empty class to which you want to make sure if it is inherited some basic members have to be implemented. This is just irrelevant to your problem.
ha wow there is no need to be so rude.
So he might need an abstract class or an interface here, we have no clue how he implemented it. I have a similar issue, but I only 24 monsters, and each monster has it's own script because they each act differently. But, there are things each monster have in common, and therefore all my individual monsters inherit from an abstract $$anonymous$$onster class. And it works, and it solves his problems, and it solves $$anonymous$$e.
It is not irrelevant, it is a working solution (and the only one posted here....)
Sorry, I was not meant to be rude. Fact is the OP does not seem to totally know what he is doing.
I thank you very much, i think i don't really understood the meaning of interfaces/abstract classes...
As a matter of fact, an interface will not fix how to find a script. This is a total other issue. His pb was he is using the same script to which he gives a different name to each of his enemies. His solution is simply to use the same script name for all the scripts. That is it. Interface are meant to define a class we want the client to implement with default members.
Hey @fafase how u doing? I'm going through a similar problem and your answer to this question has been the simplest I've ever encountered regarding getcomponent without knowing the script name. I'm building a turn-based strategy game and I want the $$anonymous$$anagerScript to get the script of whichever character I click in order to change variables on the accessed script. Can you please help me?
Suppose I create a C# script called CharacterScript on my Project folder, and then I drag it to all the character prefabs. How do I edit it differently for each instance of that script? If I double click the script on the project window and edit it there, I'll edit the "root" script and these changes will apply to all instances, right?
Or should I create each character's CharacterScript from the Inspector and save them on different folders?
Then I can simply use a selUnit.Getcomponent(); and it will get that specific character's script, no matter which one I click.
Thanks a lot in advance!
Answer by nventimiglia · Dec 02, 2012 at 06:28 PM
Try casting it?
((ScriptName)GetComponent("BlahblashBlash")).stats.AtkSpeed
I can only casting it, when i know the Scriptname, but i don't know it... I can only get the Name, as a String, of the Script.
Wait, then why are you accessing .stats ? If you don't know the component class you cant know its properties (except via reflection, but I don't think that's appropriate).
I have a Stats class, where i got all my Setters and Getters for my Stats-System, every $$anonymous$$onster get his own Script with the Name $$anonymous$$onsterName+"Points". I got an Array with all $$anonymous$$onsters and I will now sort them by a value. So i only know the ScriptName as a String.
Hold on Hold on, all you little monsters have the same script? I mean same script except the name?
Your solution does not sound eloquent or professional. I would suggest throwing it away and starting from scratch. $$anonymous$$ay I suggest have a single monobehaviour (ScoreBehaviour) with a score field. Or perhaps include the common fields in an abstract behavior. Where your heading right now is not good.
Your answer
Follow this Question
Related Questions
Passing a Script Name to a Function 2 Answers
Can i give GetComponent a variabel instead of a scriptname? 1 Answer
Help with multi menu closing using bool 1 Answer
GetComponent with variable script possible? 1 Answer
GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers