Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by harrypotterindy, Jun 26, 2015.

  1. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    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:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Player_IO : MonoBehaviour {
    6.  
    7.    RaycastHit hit;
    8.    int maxBuildDist = 7;
    9.    Transform RetAdd;
    10.    Transform RetDelete;
    11.    public GameObject block;
    12.    Color Block_Color = Color.blue;
    13.    bool Block_Menu = false;
    14.    GameObject Player_Camera;
    15.    Transform Player;
    16.    public Material grass;
    17.    public Material cobblestone;
    18.    public Material stone;
    19.    public Material diamondBlock;
    20.    public Material dirt;
    21.  
    22.    void Start () {
    23.      RetAdd = GameObject.Find ("RetAdd").transform;
    24.      RetDelete = GameObject.Find ("RetDelete").transform;
    25.      Player = GameObject.FindGameObjectWithTag ("Player").transform;
    26.      Player_Camera = GameObject.FindGameObjectWithTag("MainCamera");
    27.    }
    28.  
    29.    void Update () {
    30.      //BlockMenu stuff
    31.      if (Input.GetKeyDown (KeyCode.E)) {
    32.        Block_Menu = !Block_Menu;
    33.      }
    34.  
    35.      if (Block_Menu) {
    36.        Time.timeScale = 0;
    37.        Player.GetComponent<MouseLook>().enabled = false;
    38.        Player_Camera.GetComponent<MouseLook>().enabled = false;
    39.      }
    40.      if (!Block_Menu) {
    41.        Time.timeScale = 1;
    42.        Player.GetComponent<MouseLook>().enabled = true;
    43.        Player_Camera.GetComponent<MouseLook>().enabled = true;
    44.      }
    45.  
    46.      if (!Block_Menu) {
    47.        //Raycasting stuff
    48.        if (Physics.Raycast (Camera.main.ScreenPointToRay (new Vector3 ((Screen.width / 2), (Screen.height / 2), 0)), out hit, maxBuildDist) && !Block_Menu) {
    49.          //Making the game more sparkly
    50.          RetAdd.GetComponent<Renderer> ().enabled = true;
    51.          RetDelete.GetComponent<Renderer> ().enabled = true;
    52.          if (hit.transform.tag == "Block") {
    53.            RetAdd.transform.position = hit.transform.position + hit.normal;
    54.            RetDelete.transform.position = hit.transform.position;
    55.            RetDelete.GetComponent<Renderer> ().enabled = true;
    56.          }
    57.          if (hit.transform.tag != "Block") {
    58.            RetDelete.GetComponent<Renderer> ().enabled = false;
    59.            RetAdd.transform.position = new Vector3 (hit.point.x, hit.point.y + 0.5f, hit.point.z);
    60.          }
    61.  
    62.          //Placing blocks
    63.          if (Input.GetMouseButtonDown (1)) {
    64.            block = (GameObject)Instantiate (Resources.Load ("Block01"), RetAdd.transform.position, Quaternion.identity);
    65.          }
    66.        //Deleting blocks
    67.        else if (Input.GetMouseButtonDown (0) && hit.transform.tag != "Untagged") {
    68.            Destroy (hit.transform.gameObject);
    69.          }
    70.        } else {
    71.          RetAdd.GetComponent<Renderer> ().enabled = false;
    72.          RetDelete.GetComponent<Renderer> ().enabled = false;
    73.        }
    74.      }
    75.    }
    76.  
    77.    void OnGUI ()
    78.    {
    79.      if (Block_Menu) {
    80.        Block_Menu_GUI();
    81.      }
    82.    }
    83.  
    84.    void Block_Menu_GUI ()
    85.    {
    86.      GUILayout.BeginVertical();
    87.      if (GUILayout.Button ("Grass")) {
    88.        block.GetComponent<Renderer>().material = grass;
    89.      }
    90.      if (GUILayout.Button ("Stone")) {
    91.        block.GetComponent<Renderer>().material = stone;
    92.      }
    93.      if (GUILayout.Button ("Dirt")) {
    94.        block.GetComponent<Renderer>().material = dirt;
    95.      }
    96.      if (GUILayout.Button ("Cobblestone")) {
    97.        block.GetComponent<Renderer>().material = cobblestone;
    98.      }
    99.      if (GUILayout.Button ("Diamond Block")) {
    100.        block.GetComponent<Renderer>().material = diamondBlock;
    101.      }
    102.      GUILayout.EndVertical ();
    103.    }
    104. }
    105.  
    As always, help will be VERY MUCH appreciated. Have a great day! (And a great summer!)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. block = (GameObject)Instantiate (Resources.Load ("Block01"), RetAdd.transform.position, Quaternion.identity);
    3.  
    4. ...
    5.  
    6. block.GetComponent<Renderer>().material= grass;
    7.  
    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".
     
  3. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    I don't exactly understand, LeftyRighty. But don't judge, I'm only 10.
     
    Last edited: Jun 26, 2015
  4. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Unrelated but you should call GetComponent in start and store the results rather than calling it over and over again in update....
     
  5. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    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.:D