I am tweaking a build script for my survival game, and I want the player to only be allowed a set number of items to place. Here is the script allowing for infinite items:
using UnityEngine;
using System.Collections;
public class BuildScript : MonoBehaviour {
RaycastHit hit;
public Transform prefab;
public Transform prefab2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
if(Physics.Raycast(ray, out hit)){
if(Input.GetMouseButtonDown(1))
Instantiate(prefab, hit.point, Quaternion.identity);
if(Input.GetMouseButtonDown(0))
Instantiate(prefab2, hit.point, Quaternion.identity);
}
}
}
Anyway, now it wont let me place anything at all:
using UnityEngine;
using System.Collections;
public class BuildScript : MonoBehaviour {
RaycastHit hit;
public Transform prefab;
public Transform prefab2;
public int pref;
public int pref2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
if(Physics.Raycast(ray, out hit)){
if(Input.GetMouseButtonDown(1))
if(pref > 0)
Instantiate(prefab, hit.point, Quaternion.identity);
pref -= 1;
if(Input.GetMouseButtonDown(0))
if(pref2 > 0)
Instantiate(prefab2, hit.point, Quaternion.identity);
pref2 -= 1;
}
}
}