• 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 /
  • Help Room /
avatar image
Question by Davirtuoso · Apr 12, 2017 at 11:43 AM · c#script.getcomponentruntime

Issue referencing a method that exists in a component that is only decided at runtime

I'm in the process of building a game, and a specific issue is occurring when trying to access a method of a script/class. The primary issue is that exactly w$$anonymous$$ch script/class is going to be used will not be decided until runtime, as it will vary depending on what options a user has selected. For example, the component may be named 'Attract', 'Repel', 'Emit', 'Throw', etc. Despite t$$anonymous$$s, all of them contain the same two methods, slightly modified for each of their particular setups - 'ActionSetup()' and 'GetTriggerObjTag()'.

The contents of these methods appears to be irrelevant, as my issue is I get the following error when trying to compile:

Type 'UnityEngine.Component' does not contain a definition for 'ActionSetup' and no extension method 'ActionSetup' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?

I understand why t$$anonymous$$s is happening, because GetComponent() isn't directly defined - it is using a variable in the place of a specific component. My question is, how do you get around t$$anonymous$$s issue when you don't know the name of the component until runtime? The code of the method that calls GetComponent() in t$$anonymous$$s way is below:

 void ApplyToAll(GameObject C1, GameObject C2, GameObject Act)
 {
     // create array filled with objects matc$$anonymous$$ng tag of C1 (what it is being applied to)
     GameObject[] ObjForScript = GameObject.FindGameObjectsWithTag (C1.tag);

     // run through every object in array and...
     foreach (GameObject obj in ObjForScript) 
     {
         // check if script is already attached. If not, add it.
         MonoBehaviour testObj = obj.GetComponent (Act.name) as MonoBehaviour;
         if (testObj == null) {
             AddCorrectAction (obj, Act);
             obj.GetComponent(Act.name).ActionSetup (C2);
         }
         // if it is attached, check if the trigger is the same as what will be added.
         // if it different, add script, otherwise do not$$anonymous$$ng
         else if (testObj != null) {
             if (testObj.GetTriggerObjTag () != C2.tag) {
                 AddCorrectAction (obj, Act);
                 obj.GetComponent (Act.name).ActionSetup (C2);
             } 

             else {
                 break;
             }
         }
     }
 }

Any help would be greatly appreciated. Thankyou in advance.

Comment
Bilelmnasser

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bilelmnasser · Apr 13, 2017 at 10:06 AM

  public class componentisDecidedRuntime:MonoBehaviour
         {
          //keep reference of component in static variable
          public static componentisDecidedRuntime Instance;
         
          void Start()
             {
              Instance=t$$anonymous$$s;
              }
         public void foo()
        {
    
        }
        
  }

//to access foo;

 if(componentisDecidedRuntime.Instance!=null)
 componentisDecidedRuntime.Instance.foo();








Comment
Davirtuoso
Colocarts
Maraboo

People who like this

3 Show 1 · 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 Davirtuoso · Apr 13, 2017 at 03:10 PM 1
Share

Thankyou very much for posting.

I ended up having to manually write a few methods to handle each type of component I might need, but knowing that this is possible and how to do it will no doubt make some parts easier in the future development of my game. Thankyou for you help

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Play animation on Trigger 1 Answer

How to convert string to runnable code? 0 Answers

Difference between GetComponent 2 Answers

Activating Scripts in another Script? 2 Answers

Unity GetComponent Script? 1 Answer


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