Hi,
I was making a minecraft game and trying to add an inventory like system sort of like what you use in creative mode. In my script I just check if I press different buttons, then change the material of the block I place. However, it does not want to work that way. When I place, for example, a diamond block, then want to place a different block, my UI pops up, and then I press, let’s say, the dirt block button. Then the diamond block that I just placed changes it’s material to dirt, and when I try to place another dirt block, it places diamond blocks instead. Here is my code:
using UnityEngine;
using System.Collections;
public class Player_IO : MonoBehaviour {
RaycastHit hit;
int maxBuildDist = 7;
Transform RetAdd;
Transform RetDelete;
public GameObject block;
Color Block_Color = Color.blue;
bool Block_Menu = false;
GameObject Player_Camera;
Transform Player;
public Material grass;
public Material cobblestone;
public Material stone;
public Material diamondBlock;
public Material dirt;
void Start () {
RetAdd = GameObject.Find ("RetAdd").transform;
RetDelete = GameObject.Find ("RetDelete").transform;
Player = GameObject.FindGameObjectWithTag ("Player").transform;
Player_Camera = GameObject.FindGameObjectWithTag("MainCamera");
}
void Update () {
//BlockMenu stuff
if (Input.GetKeyDown (KeyCode.E)) {
Block_Menu = !Block_Menu;
}
if (Block_Menu) {
Time.timeScale = 0;
Player.GetComponent<MouseLook>().enabled = false;
Player_Camera.GetComponent<MouseLook>().enabled = false;
}
if (!Block_Menu) {
Time.timeScale = 1;
Player.GetComponent<MouseLook>().enabled = true;
Player_Camera.GetComponent<MouseLook>().enabled = true;
}
if (!Block_Menu) {
//Raycasting stuff
if (Physics.Raycast (Camera.main.ScreenPointToRay (new Vector3 ((Screen.width / 2), (Screen.height / 2), 0)), out hit, maxBuildDist) && !Block_Menu) {
//Making the game more sparkly
RetAdd.GetComponent<Renderer> ().enabled = true;
RetDelete.GetComponent<Renderer> ().enabled = true;
if (hit.transform.tag == "Block") {
RetAdd.transform.position = hit.transform.position + hit.normal;
RetDelete.transform.position = hit.transform.position;
RetDelete.GetComponent<Renderer> ().enabled = true;
}
if (hit.transform.tag != "Block") {
RetDelete.GetComponent<Renderer> ().enabled = false;
RetAdd.transform.position = new Vector3 (hit.point.x, hit.point.y + 0.5f, hit.point.z);
}
//Placing blocks
if (Input.GetMouseButtonDown (1)) {
block = (GameObject)Instantiate (Resources.Load ("Block01"), RetAdd.transform.position, Quaternion.identity);
}
//Deleting blocks
else if (Input.GetMouseButtonDown (0) && hit.transform.tag != "Untagged") {
Destroy (hit.transform.gameObject);
}
} else {
RetAdd.GetComponent<Renderer> ().enabled = false;
RetDelete.GetComponent<Renderer> ().enabled = false;
}
}
}
void OnGUI ()
{
if (Block_Menu) {
Block_Menu_GUI();
}
}
void Block_Menu_GUI ()
{
GUILayout.BeginVertical();
if (GUILayout.Button ("Grass")) {
block.GetComponent<Renderer>().material = grass;
}
if (GUILayout.Button ("Stone")) {
block.GetComponent<Renderer>().material = stone;
}
if (GUILayout.Button ("Dirt")) {
block.GetComponent<Renderer>().material = dirt;
}
if (GUILayout.Button ("Cobblestone")) {
block.GetComponent<Renderer>().material = cobblestone;
}
if (GUILayout.Button ("Diamond Block")) {
block.GetComponent<Renderer>().material = diamondBlock;
}
GUILayout.EndVertical ();
}
}
As always, help will be VERY MUCH appreciated. Have a great day! (And a great summer!)