Help Please! Material changing system not working in my Minecraft like game!

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!)

block = (GameObject)Instantiate (Resources.Load ("Block01"), RetAdd.transform.position, Quaternion.identity);

...

block.GetComponent<Renderer>().material= grass;

you are updating the material of the block you just placed. If you want to select the block type first, then instantiate a block of that type you’ll need to have a variable for “selectedMaterial” which is updated in your Block_Menu_UI() function (instead of updating the renderer of the block you just placed) and when you instantiate the block on a user leftclick you need to set the renderer then, to the “selectedMaterial”.

I don’t exactly understand, LeftyRighty. But don’t judge, I’m only 10.

Unrelated but you should call GetComponent in start and store the results rather than calling it over and over again in update…

Honestly, guys, I appreciate the help, but I just don’t understand. If you have an answer and now exactly what to do with it and how to deal with it, please just copy my script and then just edit it and post back the edited versoin that will work right because I don’t understand what you are trying to tell me. If you could write out what you are telling me in words in script instead, I would very much appreciate it. As always, thanks for the help and support.:smile: