- Home /
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.
Access it with GetComponent. You can use this to fire Functions from other scripts or read/write variables.
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();
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.
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" );
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.
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.
Your answer
Follow this Question
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