- Home /
Why can't void be used in this context?
Why isn't void start() working here? Whats going wrong here?
using System.Collections;
public class DetectGridGrassland : MonoBehaviour {
public int randNum = 0;
void OnTriggerEnter(Collider other){
Debug.Log("Player entered the Grass Land");
void Start(){
randNum = Random.Range(0,5);
}
void OnGUI(){
if (randNum <= 3)
{
GUI.Button(new Rect(10, 10, 125, 25), "Attacked by bandits");
}
else if(randNum == 3)
{
GUI.Button(new Rect(10, 10+20, 125, 25), "Attacked by wolves");
}
else
{
GUI.Button(new Rect(10, 10+30, 125, 25), "Nothing to do!");
}
}
}
}
Comment
Best Answer
Answer by ICHeeryI · Feb 20, 2014 at 09:58 PM
No you can not use void in void(method in method). You check if player entered grass land, but now you want to tell other methods that player entered grass land. Just create new variable
bool enter = false;
So when player enter grass land set it to true, when he leave set it to false.
using System.Collections;
public class DetectGridGrassland : MonoBehaviour {
public int randNum = 0;
public bool enter = false;
void OnTriggerEnter(Collider other){
Debug.Log("Player entered the Grass Land");
enter = true; //We entered grass land, set enter to true
}
void OnTriggerExit(Collider other){
enter = false;//We left grass land, set enter to false
}
void Start(){
randNum = Random.Range(0,5);
}
void OnGUI(){
//Check if enter is true
if(enter == true){
if (randNum <= 3)
{
GUI.Button(new Rect(10, 10, 125, 25), "Attacked by bandits");
}
else if(randNum == 3)
{
GUI.Button(new Rect(10, 10+20, 125, 25), "Attacked by wolves");
}
else
{
GUI.Button(new Rect(10, 10+30, 125, 25), "Nothing to do!");
}
}
}
}
This worked perfectly. Thank you for explaining this to me!
Answer by CodeElemental · Feb 20, 2014 at 09:38 PM
Missing closing bracket for this method :
void OnTriggerEnter(Collider other){
Debug.Log("Player entered the Grass Land");
It's not missing.. (but Start and OnGUI are declared inside it)