- Home /
Help with clearing bugs in this C# script
Hey I need help. I made a script and Im having allot of troubles. When I press "I" It unlocks the mouse and brings up the GUI spawn menu and when I press it again it it closes but if I click on a button and spawn something it locks the mouse again and When I press "I" it unlocks the mouse again but it doesnt hide the buttons again also I want the Mouse look script to be disabled when the spawn menu is showing and enabled when its being hidden. How do I fix this (including the mouse locking again when something is spawned) Here is the script:
using UnityEngine;
using System.Collections;
public class SpawnGui : MonoBehaviour
{
private bool showGUI;
public bool lockCursor = true;
public Transform Cube;
public Transform Cuboid;
public Transform BlockHead;
public bool CubeC;
public bool CuboidC;
public bool BlockHeadC;
void OnMouseDown ()
{
}
void Start ()
{
showGUI = false;
Screen.lockCursor = true;
if(Input.GetKeyDown(KeyCode.Escape))
{
Application.LoadLevel(0);
}
}
void Update ()
{
if(CuboidC)
{
if (Input.GetMouseButtonDown(1))
renderer.material.color = Color.red;
if (Input.GetMouseButtonUp(1))
{
Destroy(gameObject);
}
}
if(CubeC)
{
if (Input.GetMouseButtonDown(1))
renderer.material.color = Color.red;
if (Input.GetMouseButtonUp(1))
{
Destroy(gameObject);
}
}
if (BlockHeadC)
{
if (Input.GetMouseButtonDown(1))
renderer.material.color = Color.red;
if (Input.GetMouseButtonUp(1))
{
Destroy(gameObject);
}
}
if(Input.GetKeyDown(KeyCode.I))
{
if(showGUI == true)
{
showGUI = false;
Screen.lockCursor = true;
}
else if(showGUI == false)
{
showGUI = true;
Screen.lockCursor = false;
}
}
}
void OnGUI()
{
if(showGUI == true)
{
if(GUI.Button(new Rect(20,20,100,80), "Cube"))
{
Instantiate(Cube,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
}
if(GUI.Button(new Rect(20,120,100,80), "Cuboid"))
{
Instantiate(Cuboid,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
}
if(GUI.Button(new Rect(20,220,100,80), "BlockHead"))
{
Instantiate(BlockHead,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
}
}
}
}
I didn't vote you down, but the explanation in your question consists of a single uncomprehensible sentence that goes on and on over 5 lines. Plus, a script dump, while sometimes helpful/necessary, means everybody needs to invest considerable time to try and understand your code before they can try to answer.
Let's slim things down some, shall we?
public GameObject cube;
public GameObject cuboid;
public GameObject blockHead;
public Transform spawnPoint;
private GameObject
cubeInstance,
CuboidInstance,
blockHeadInstance;
void Awake()
{
Screen.lockCursor = true;
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
Application.LoadLevel(0);
if (cubeInstance != null && Input.Get$$anonymous$$ouseButtonDown(1))
StartCoroutine(DestroyObject(cubeInstance));
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
Screen.lockCursor = !Screen.lockCursor;
}
void OnGUI()
{
if (Screen.lockCursor) return;
if (GUI.Button(new Rect(20,20,100,80), "Cube"))
cubeInstance = Instantiate(cube, spawnPoint.position, Quaternion.identity) as GameObject;
}
IEnumerator DestroyObject(GameObject go)
{
const float TI$$anonymous$$E_TO_LIVE = 0.25f
go.renderer.material.color = Color.red;
yield return new WaitForSeconds(TI$$anonymous$$E_TO_LIVE)
if (go != null)
Destroy(go);
}
I didn't put in all the object types that you have; it'll be quite simple to get them in there. This might have even solved your problem; and now you can actually load the next level by pressing the escape key. ;)
Plus, it might be beneficial to use phrases such as "Hi there! Could somebody help me out please?", ins$$anonymous$$d of "Hey I need help."...
This ^ but anyone down voting without reason doesn't help him either.
Answer by T27M · Feb 02, 2013 at 09:09 PM
Could you explain your first problem a little more I'm not seeing the problem you say you encounter when you click the GUI button.
Assuming that the MouseLook script is attached to the same gameobject as this script you can use
if(Input.GetKeyDown(KeyCode.I))
{
if(showGUI == true)
{
showGUI = false;
Screen.lockCursor = true;
gameObject.GetComponent<MouseLook>().enabled = true;
}
else if(showGUI == false)
{
showGUI = true;
Screen.lockCursor = false;
gameObject.GetComponent<MouseLook>().enabled = false;
}
}
If not you will have to use a reference to the MouseLook component.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Random element from the list 1 Answer
Character refuses to turn 0 Answers