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 /
avatar image
0
Question by edve98 · Oct 11, 2014 at 05:16 PM · c#gameobject

Using script's method from all of the gameobjects that has that script

The name pretty much says it all. I need to run one method from a script in all gameobjects that have that script in them(if this changes anything: I need to make this work from an RPC). How could I do that?

P.s. C# only please

Comment
Add comment
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

2 Replies

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

Answer by plainerman · Oct 11, 2014 at 07:01 PM

There are several ways to do this! In my opinion the most beautiful way to do this would be to make a manager. Make a script called MyScriptManager.cs

 using UnityEngine;
 using System.Collections;
 
 public class MyScriptManager: MonoBehaviour
 {
  public static List<MonoBehaviour> myScripts = new List<MonoBehaviour>();
 
  public static void AddNewEntry(MonoBehaviour mono)
  {
   if(mono!=null)
    list.add(mono);
  }
 ...
 }

You may have to add using System.Collections.Generic;

Afterwards you can add to your Script:

 using UnityEngine;
 using System.Collections;
 
 public class MyScript: MonoBehaviour
 {
 ...
  void Awake()
  {
   MyScriptManager.AddNewEntry(this);
  }
 }

And whenever you want to call the function on all Scripts use this in your MyScript class:

 void Update()
 {
  if(something)
  {
   foreach(MonoBehaviour mono in MyScriptManager.myScripts)
   {
    ((MyScript)mono).callAwesomeFunction();
   }
  }
 }

You may can't copy + paste because I wrote it without compiler! Hope this solves your problem!

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 edve98 · Oct 11, 2014 at 07:09 PM 0
Share

Thanks for answering! I thought that there might be an easier way to do this, but looks like not :/

avatar image
1

Answer by Vsenik · Oct 11, 2014 at 07:00 PM

There are several scenarios. It is unclear about what you really wanna do. But if I undestand you correctly then you could use script made by Cerebrate

Simple example of usage:

 public interface ICanDoSomeAction
 {
     void SomeAction(); 
 }
 
 class Digger : MonoBehaviour,ICanDoSomeAction
 {
     public void SomeAction()
     {
         Dig();
     }
 
     private void Dig()
     {
         Debug.Log(gameObject.name + " digs");       
     }
 }
 
 class Archer : MonoBehaviour,ICanDoSomeAction
 {
     public void SomeAction()
     {
         Shoot();
     }
 
     private void Shoot()
     {
         Debug.Log(gameObject.name + " make  a shot");       
     }
 }
 
 class Manager:MonoBehaviour
 {
     public GameObject[] Actors;
     private ICanDoSomeAction[] actions;
 
     public 
     void Start()
     {
         actions = new ICanDoSomeAction[Actors.Length];
         for (int i = 0; i < Actors.Length; i++)
         {
             actions [i] = Actors [i].GetInterface<ICanDoSomeAction>();
         }
            
     }
 
     void Update()
     {
         foreach (var a in actions)
             a.SomeAction();
     }
 }

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 edve98 · Oct 11, 2014 at 07:11 PM 0
Share

Thank you! Yours and playerman's answers are great, sorry that I did not chose yours as the correct one, but I had to choose...

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

30 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

Related Questions

Camera to follow a target within a circle? 1 Answer

Assigning current color to a variable for fade out (C#) 0 Answers

C# Gameobject's Script's ValuesEquals Other Gameobject's script's Values 1 Answer

NullReference when accessing GameObject in array (C#) 1 Answer

Getting nullreference error while using sendmessage 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