- Home /
Question by
WeirdDeveloping · Feb 03, 2016 at 10:46 PM ·
scripting problem
Collision Detecting not working!
I Have made a script, When the Game Object has collided with a object a menu appears
here is the script sing UnityEngine; using System.Collections;
public class RaceScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col)
{
if (col.collider.gameObject.name == "TestSegway")
{
if (col.gameObject.name == "TestSegway") {
GUI.Box(new Rect(10,10,100,90), "You have won! Congrats! :D");
if(GUI.Button(new Rect(20,40,80,20), "Home Menu")) {
Application.LoadLevel(1);
}
}
}
}
}
Comment
Answer by allenallenallen · Feb 03, 2016 at 10:52 PM
There are many things wrong with your code.
First of all, you're using the old GUI code so you have to write them in OnGUI(): http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html
Secondly, you checked the GameObject name twice in a row for no reason whatsoever.
I recommend you to use a boolean for the GUI to check whether it can show up or not.
bool showGui = false;
void OnGUI(){
if (showGui) {
GUI.Box(new Rect(10,10,100,90), "You have won! Congrats! :D");
if(GUI.Button(new Rect(20,40,80,20), "Home Menu")) {
Application.LoadLevel(1);
}
}
}
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "TestSegway"){
showGui = true;
}
}