Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
0
Question by KaspianR · May 02, 2018 at 08:55 AM · script.functionfunctionsmultiple-objects

Call same function in multiple scripts

Hi! I have six different types of cameras with their own respective script. All the scripts have a function called "CheckForPlayer" wich returns a bool based on if the camera can see the player or not. I also have a game manager that is supposed to loop through an array containing all the cameras and if one of the cameras return true the player will lose. But my problem is that i can't figure out how to store different scripts in the same array and then call the function. Is that even possible?

Thanks in regard!

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 kskjadav007 · May 02, 2018 at 10:41 AM 0
Share

Just check out This

4 Replies

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

Answer by Harinezumi · May 02, 2018 at 09:18 AM

What you are looking for is called polymorphism, a very important topic in programming, which in C# can be done with interfaces or subclassing.

Basically, you can need to create a common base type which all of your camera scripts implement or derive from, and then in your game manager store your cameras in an array of that type. It can be either an interface or an abstract base class (there are some differences, but that's not important for now). Something like this:

 public interface PlayerChecker {
     void CheckForPlayer(); // function that must be public and without implementation, but it can have whatever return type and parameters you want
 }

 // or you can do this:
 public abstract class PlayerChecker : MonoBehaviour {
     public abstract void CheckForPlayer (); // very similar to the above
 }
 
 // do the same for your other camera classes
 // if you use the abstract base class, remove "MonoBehaviour,", because you already subclass it
 public class YourCameraScriptVersion1 : MonoBehaviour, PlayerChecker {
 
     // here implement the interface
     public void CheckForPlayer () { /* do whatever this camera does */ }

     // or here implement the abstract base class. The override keyword tells the compiler that the name is intentional, not named the same as in the base class by mistake
     public override void CheckForPlayer () { /* do whatever this camera does */ }

 }
 
 public class GameManager : MonoBehaviour {
 
     // put in this array exposed on the Editor your implementors of PlayerChecker, that is your cameras
     [SerializeField] private PlayerChecker[] playerCheckers = null;
 
     private void ChecksForPlayer () {
         if (playerCheckers != null) {
             // now you can call the same function on all cameras, because they all implement the PlayerChecker interface
             for (int i = 0; i < playerCheckers.Length; ++i) { playerCheckers[i].CheckForPlayer(); }
         }
     }
 }

Programming-wise there is no significant difference between the 2 approaches, but in Unity if you want to be able to assign the base type in the Editor, you need to use the abstract base class approach.
The limitation of this is that you need to create an interface (functions with parameters) that is good for all of your camera implementations. Try to think about it the way that you cannot know what is the real type, it should work no matter which one it is.

Comment
Add comment · Show 2 · 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 KaspianR · May 02, 2018 at 04:15 PM 1
Share

Thanks for the easy and correct answer! I accepted your answer since it is easier to follow along with than the other answers although they are correct as well!

avatar image Harinezumi KaspianR · May 02, 2018 at 08:48 PM 0
Share

Thanks! I personally liked Bunny83's answer more :D

avatar image
3

Answer by Hellium · May 02, 2018 at 09:09 AM

Yes it is possible.

I see two possibilities:

    1. Inheritance :

            1. Create an abstract class (called CameraScript for instance), and define the abstract method returning the boolean

            2. Make your camera scripts inherit from the abstract class

            3. In your game manager, define the array as follow : public CameraScript[] CameraScripts


    2. Interface :

            1. Create an interface class (called ICameraScript for instance), and define the method returning the boolean

            2. Make your camera scripts implement the interface

            3. In your game manager, define the array as follow : public GameObject[] CameraScripts

            4. When you want to check the condition, call GetComponent<ICameraScript>().YourMethod()

In fact, Unity is not able to serialize interfaces, explaining why you have to define an array of GameObjects, and not an array of interfaces

Comment
Add comment · Show 4 · 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 KaspianR · May 02, 2018 at 09:14 AM 0
Share

Oh! That seems to be the way to go, I will try it out as soon as i get back at my computer! Thanks for the fast reply!

avatar image Bunny83 · May 02, 2018 at 09:17 AM 1
Share

I'm getting slow ^^ +1

avatar image goodgamershow · Jul 05, 2021 at 09:14 AM 0
Share

So I attached gameobjects into the GameObject[] array and when running GetComponent().Death(); I get "Object reference not set to an instance of an object error"

avatar image Hellium goodgamershow · Jul 05, 2021 at 09:53 AM 0
Share

It means the gameobjects you've provided don't have the component (or a component implementing the interface) you're looking for. Or one of the gameObjects is null.

avatar image
1

Answer by Bunny83 · May 02, 2018 at 09:17 AM

First of all if the check if the player is visible in a certain camera is actually the same for each camera you should have created a seperate component and attach it to each camera. If you actually do something different in your "CheckForPlayer" method in each script you can simply use polymorphism.


If you want to be able to setup the references in the inspector you would need to derive all your scripts from an abstract base class which implements an abstract "CheckForPlayer" method that will be overridden in each concrete implementation of your scripts. That way you can have a List of that base class, assign all cameras in the inspector.


If you actually setup the references at runtime using GetComponent you can simply use an interface like this:

 public interface IPlayerDetector
 {
     bool CheckForPlayer();
 }

And then just let each of your script implement that interface:

 public class SomeScript : MonoBehaviour, IPlayerDetector
 {
     // [ ... ]
     public bool CheckForPlayer()
     {
     }
 }


You can just do GetComponent<IPlayerDetector>() and store them in your List

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
0

Answer by KaspianR · May 03, 2018 at 06:51 AM

Thanks for all the great answers! These answers are easy to both implement and to understand. Personally I prefer the abstract class since it looks better in the inspector! I accepted Harinezumi's answer because it contains code that you can just copy paste in to your own project!

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

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

92 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

Related Questions

BCE0019 when using functions 0 Answers

Fill out Event Trigger from Script 0 Answers

Problem with multiple objects using the same script 0 Answers

How to randomize the correct choice 2 Answers

Function parameter cant be referenced 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