Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by cobertos · Mar 17, 2013 at 04:29 PM · c#arrayfunctionsendmessage

How to call a function from another script in C# from array?

Here's the syntax error I get:

Assets/ScriptsNEW/PlayerController.cs(41,42): error CS1061: Type UnityEngine.MonoBehaviour' does not > contain a definition for deActivate' and no extension method deActivate' > of type UnityEngine.MonoBehaviour' could be found (are you missing a using directive or an assembly reference?)

Here's some of the code:

 private MonoBehaviour[] modes = new MonoBehaviour[3];

 void Start()
 {
     modes[0] = (ModeA)this.GetComponent("ModeA");
     modes[1] = (ModeB)this.GetComponent("ModeB");
     modes[1].enabled = false;
     modes[2] = (ModeC)this.GetComponent("ModeC");
     modes[2].enabled = false;
 }


 void setMode(int s)
 {
     if(currMode==s){return;}
     for(int i=0; i<modes.Length; i++)
     {
         if(modes[i].enabled == true)
         {
             modes[i].deActivate();
             modes[i].enabled = false;
         }
     }
     modes[s].enabled = true;
     currMode = s;
 }

Basically, what I want this to do is when it calls setMode, it deActivates all the other scripts. Because each script is a class all its own, I have to store them in an array of MonoBehaviour. Each script has the deActivate function but it won't work because its not default to MonoBehaviour. Do I have to make a new class of scripts called like, "Mode", or something and give them this deActivate function and then inherit from that for all these mode scripts. Is there a better way I'm missing?

Comment
Add comment · Show 1
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 · Mar 17, 2013 at 05:34 PM 0
Share

But are you trying to deactivate the game object it is attached to?

1 Reply

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

Answer by pazzesco · Mar 17, 2013 at 05:10 PM

Probably the most sound way would indeed be to make a super class of "Mode", with an empty deActivate function that you can override and craft to your will. If you want to call the function on every MonoBehaviour residing on a GameObject, you could use SendMessage. In this case, you could make a parameter for deActivate on each of your "Mode" components that uniquely identifies that component. Something like this could work:

 void setMode(int s)
 {
     if(currMode==s){return;}
     this.SendMessage("deActivate", modes[s].GetType().Name, SendMessageOptions.DontRequireReceiver);
 }
 
 
 //...
 //the deActivate function on each of your "Mode" MonoBehaviours
 //...
 
 void deActivate(string stayActiveTypeName) {
     if (stayActiveTypeName != this.GetType().Name) {
         //do deactivating stuff here
     }
 }

The problem with doing it the way I've shown is that you're going to have to repeat code for each Mode MonoBehaviour. You want to avoid that. But, unless you really hate inheritance, I'd suggest sticking with your original idea.

P.S. There is possibly a more sound method to using SendMessage in this manner. Personally, I liked your solution that inherits a base "Mode" type. It'd be very clean.

Comment
Add comment · 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 pazzesco · Mar 17, 2013 at 05:20 PM 0
Share

Also note that Send$$anonymous$$essage will try to call the deActivate method on ALL of your $$anonymous$$onoBehaviours on a particular GameObject. Things could get messy if you end up using the name "deActivate" on other components. You could elaborate the function names, such as "deActivate$$anonymous$$ode."

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

12 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

Related Questions

How to make a List or Array of functions with type Void 4 Answers

How to call a function with Vector3[] argument? 3 Answers

What is the best way to call a function from another script 1 Answer

Sending a message from javascript to C# 1 Answer

Multiple Cars not working 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