- Home /
Calling A Method From Another Script in C Sharp
I'm trying to call a method 'GameOver();' from a script 'GameMaster.'
Here's my code:
UILabel highScore = GameObject.Find("HighScore").GetComponent<UILabel>();
highScore.text = "High Score: " + GetComponent<GameMaster>().GameOver();
As you can see on the second line of my code I'm using a GetComponent to call the script and then I reference the method.
I get an error that says Assets/nguiScore.cs(25,78): error CS0122: `GameMaster.GameOver()' is inaccessible due to its protection level.
I would like help. Thank you for your time and have a nice day.
Answer by tyrike thompson · Jul 30, 2014 at 01:28 AM
The GameOver method is probably "private" , try changing it to public.
Answer by robertbu · Jul 30, 2014 at 01:18 AM
The GameOver() method must have have the keyword 'public' in front of the method declaration to be used this way from another script. Something line:
public void GameOver() {
Beat me by 16 seconds! Damn I shouldn't have stopped for that tater-tot.