Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 HBishop · May 14, 2021 at 09:13 PM · staticfunction callreuse

Reusable function in multiple scenes

I have a simple function in scene 1 that checks to see if i can spawn that gameobject (check if player is there) I need this function to be usable on other gameobjects in other scenes. How would i go about that? It is currently a monobehavior on an empty object that gameobjects in scene 1 call.

The initial plan was to put that empty object with script into all scenes but wasn't sure if there was a cleaner way.

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
1
Best Answer

Answer by Isaac_Medina225 · May 14, 2021 at 11:40 PM

@HBishop You may use static modifier:

  public class TestFunctions : MonoBehaviour
     {
 
         // This is how I make a method that I can use on other class
         public static float ReusableFunction(float num)
         {
             return num * num; 
         }
 
         // This is how I make a method that I can use on other class
         public static void ReusableMethod() {
             Debug.Log("Hello World");
         }
     }
 

Other class call methods and functions:

 public class OtherClass : MonoBehaviour
 {
     private float someVariable;
 
     void Start()
     {
 
         someVariable = TestFunctions.ReusableFunction(0.5f);
 
         Debug.Log(someVariable);
         TestFunctions.ReusableMethod();
     }
 }


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 HBishop · May 15, 2021 at 09:47 AM 0
Share

Would the test function class be on a gameobject in the scene. Or is it a static class that isn't attached? Which would be better? Thank you for your really fast and concise response!

avatar image Isaac_Medina225 · May 15, 2021 at 04:17 PM 1
Share

You don't need to put the TestFunction class on a game object you can call on other class. it happens because of the static you may try adding static on the function that you want to call on other class :)

avatar image HBishop Isaac_Medina225 · May 15, 2021 at 10:08 PM 0
Share

I thought all monobehaviour classes had to be attached to a gameobject. How does the static keyword in front of the function make this not needed to be attached for the Test Class?

Just had a go now I'm back at my computer and it currently keeps popping up with an error saying NullReference: Not set to an instance of an object

Thanks :)

avatar image Isaac_Medina225 HBishop · May 16, 2021 at 09:53 AM 0
Share

You can still call it even if you don't put it on the game object since you put static keyword on your function.

You just need to type your class name that has the static function. In the code above that I provide, I can call the function on the other class without putting the TestFunctions class on the gameobject. I call it's function using TestFunctions.anyPublicFunctionWithStaticKeyword

Show more comments
avatar image
0

Answer by logicandchaos · May 16, 2021 at 01:54 AM

If you just have it on an empty gameObject why make it a monobehaviour at all? You could make your own class and make it static and use it anywhere without having to worry about gameObjects.

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 HBishop · May 16, 2021 at 09:22 AM 0
Share

Thats great, sounds less messy than having gameobjects everywhere

Public TestFunction{

public static void ResuableMethod() { Debug.Log("test") } }

Like that? Thank you! :)

avatar image Isaac_Medina225 HBishop · May 16, 2021 at 09:57 AM 1
Share

Yes, you can do that too, in my case, its working even if I didn't remove monobehavior

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

120 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

Related Questions

How does one inspect static vars? 3 Answers

Problem with Static Variable? 1 Answer

Non-Static Accessibility 1 Answer

Help with Error: An instance of type 'Regex' is required to access non static member 'Replace'. 2 Answers

Static function, coroutine and trying to access an instance 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