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 KG3 · Nov 17, 2014 at 06:01 PM · functionstatic

Using a Function from another class without it being Static?

Hey guys I am trying to call a function called score in a different class without making the score function static.

I can call GameManager.score(wallName); for example, but the score function needs to be static.

 using UnityEngine;
 using System.Collections;
 
 public class SideWalls : MonoBehaviour {
 
     // Update is called once per frame
     void OnTriggerEnter2D(Collider2D hitInfo) 
     {
         if (hitInfo.name == "Ball")
         {
             string wallName = transform.name;
             audio.Play();
             audio.pitch = Random.Range(0.8f, 1.2f);
             GameManager.score(wallName);
 
             hitInfo.gameObject.SendMessage("ResetBall");
         }
 
     }
 }


I am trying to make the function work in a non-Static way. So instead of declareing the score function "static void score()" I want it to work without it being static. like this.

 public class GameManager : MonoBehaviour {
 
    int playerScore01 = 0;
    int playerScore02 = 0;
    public GUISkin theSkin;
    Transform theBall;
 
    void Start()
    {
        theBall = GameObject.FindGameObjectWithTag("Ball").transform;
    }
 
     // I want this function to work without it being Static!
      public void score (string wallName)
      {
          if (wallName == "rightWall")
          {
              playerScore01++;
          }
          else
          {
              playerScore02++;
          }
          Debug.Log("Player Score 1 is " + playerScore01);
          Debug.Log("Player Score 2 is " + playerScore02);
     }
 
 }


I tried every work around possible, but I can't get it to work.

Comment
Add comment · Show 4
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 meat5000 ♦ · Nov 17, 2014 at 06:07 PM 1
Share

Access it with GetComponent. You can use this to fire Functions from other scripts or read/write variables.

avatar image KG3 · Nov 17, 2014 at 06:19 PM 0
Share

I tried getcomponent in the function above. It gives me the same error

so I did: public class SideWalls : $$anonymous$$onoBehaviour {

 Game$$anonymous$$anager score = GetComponent<Game$$anonymous$$anager>();
 
      // Update is called once per frame
      void OnTriggerEnter2D(Collider2D hitInfo) 
      {
          if (hitInfo.name == "Ball")
          {
              string wallName = transform.name;
              audio.Play();
              audio.pitch = Random.Range(0.8f, 1.2f);
              score.score(wallName);
  
              hitInfo.gameObject.Send$$anonymous$$essage("ResetBall");
          }
  
      }
  }
 

The error I got was a Object Reference was required for the non-static field, method, or UnityEngine.Componet.GetComponet();

avatar image meat5000 ♦ · Nov 17, 2014 at 07:16 PM 0
Share

I should add that something need only be public for another script to access it. As long as you first 'Find' the object you can GetComponent to its scripts.

avatar image KG3 · Nov 19, 2014 at 02:15 PM 0
Share

@meat5000 okay I see what you mean.

2 Replies

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

Answer by Rebaken-Enterprises · Nov 17, 2014 at 06:26 PM

Currently it needs to be static because you are calling it on the Class itself and not an instance of that class.

You could look up Singletons on how to create an instance of GameManager that is stored right inside GameManager. Then you can call of it's functions on that instance. I'm just typing this off the top of my head, it probably doesn't work, but might get you the idea, for you GameManager class:

     public class GameManager : MonoBeahviour {
     
     public static GameManager The_One_And_Only_GameManager;
     
     void Awake() {
         if( The_One_And_Only_GameManager == null ) {
              The_One_And_Only_GameManager = this;   // If there wasn't an instance before, make this the instance
         DontDestoryOnLoad( gameObject ); // Keep this puppy around forever
       }  else if( this != The_One_And_Only_GameManager ) {
              Destroy(this); // If The_Only_And_Only has already been set, and this instance isn't it, destory this instance
        }
     }
 }

Attach your GameManager to an empty GameObject, then from other scripts you use: GameManager.The_One_And_Only_GameManager.Score( "FrontWall" );

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 KG3 · Nov 19, 2014 at 02:14 PM 1
Share

Yeah I talk to my professor and a friend yesterday. He said using a singleton was the best solution. So I am using this tutorial to $$anonymous$$ch me about them. http://unitypatterns.com/singletons/ its really helpful.

avatar image
0

Answer by spiceboy9994 · Nov 17, 2014 at 06:25 PM

Hi:

You can do something like this on your SideWalls Behavior:

 void OnTriggerEnter2D(Collider2D hitInfo) 
      {
          if (hitInfo.name == "Ball")
          {
              string wallName = transform.name;
              audio.Play();
              audio.pitch = Random.Range(0.8f, 1.2f);
              /*If your GameManager Behavior is attached to this same object, uncomment this line*/
              /*this.GetComponent<GameManager>().score(wallName);*/
 /*If your GameManager Behavior is attached to other gameObject, then add a reference to the external game object and reference to the gamebehavior from that object*/
              /*externalGameObject.GetComponent<GameManager>().Score(wallName); //considering that externalGameObject is the external object
              GameManager.score(wallName);*/
  
              hitInfo.gameObject.SendMessage("ResetBall");
          }
  
      }

Hope this helps.

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 KG3 · Nov 19, 2014 at 02:16 PM 0
Share

I tired both of these solutions. They did not work.

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

29 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

Related Questions

Coroutine cannot be automatically started from a static function 1 Answer

Get a public variable to the function. 3 Answers

using non static variable in static function 1 Answer

Acces an Awake variable 2 Answers

static function? 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