What I wanted to do was after pressing the spawn button I can click anywhere else after that to specify where my object would spawn but the problem is that it doesn’t work with:
if(Input.GetMouseButtonDown(0))
Here’s my code for the GUI spawning
using UnityEngine;
using System.Collections;
public class myGUI : MonoBehaviour {
RaycastHit hit;
public GameObject prefab;
int inc = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow);
Physics.Raycast(ray, out hit, 100);
}
void Ispawn(){
//Here I placed an increment so the spawn limit is 10
if(inc < 10){
//Here's where i stated where I want the object to spawn. Spawn location is where the mouse is.
Instantiate(prefab, new Vector3(hit.point.x,hit.point.y,hit.point.z),Quaternion.identity);
inc++;
}
}
void OnGUI() {
//GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "This is a title");
// Make a background box
GUI.Box (new Rect (10,10,100,90), "Spawn Menu");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed <- LIES
if (GUI.Button (new Rect (20,40,80,20), "Spawn")) {
Ispawn();
}
// Make the second button.
if (GUI.Button (new Rect (20,70,80,20), "Spawn 2")) {
print("You press ME!");
}
}
}
So what Am I supposed to do?