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.

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” );

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.