Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by OlMon · Dec 02, 2012 at 06:25 PM · getcomponentstringdelegate

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

Comment
Add comment · Show 2
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 fafase · Dec 02, 2012 at 06:40 PM 0
Share

What if you tried a dictionary string/type and then you can easily use a string to find the corresponding class.

avatar image OlMon · Dec 02, 2012 at 06:52 PM 0
Share

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....

2 Replies

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

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!

Comment
Add comment · Show 5 · 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 OlMon · Dec 02, 2012 at 08:47 PM 0
Share

I thank you very much, i think i don't really understood the meaning of interfaces/abstract classes...

avatar image fafase · Dec 03, 2012 at 07:12 AM 0
Share

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.

avatar image edthethird · Dec 03, 2012 at 11:04 PM 0
Share

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....)

avatar image fafase · Dec 04, 2012 at 06:46 AM 0
Share

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.

avatar image Plajen · Jun 01, 2019 at 07:59 PM 0
Share

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!

avatar image
0

Answer by nventimiglia · Dec 02, 2012 at 06:28 PM

Try casting it?

((ScriptName)GetComponent("BlahblashBlash")).stats.AtkSpeed

Comment
Add comment · Show 8 · 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 OlMon · Dec 02, 2012 at 06:38 PM 0
Share

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.

avatar image nventimiglia · Dec 02, 2012 at 08:10 PM 0
Share

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).

avatar image OlMon · Dec 02, 2012 at 08:26 PM 0
Share

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.

avatar image fafase · Dec 02, 2012 at 08:30 PM 0
Share

Hold on Hold on, all you little monsters have the same script? I mean same script except the name?

avatar image nventimiglia · Dec 02, 2012 at 08:32 PM 0
Share

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.

Show more comments

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

14 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 avatar image avatar image avatar image

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


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