Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problems with if statement

Discussion in 'Scripting' started by unity_MDSWhX3wDG-uiw, Sep 7, 2019.

  1. unity_MDSWhX3wDG-uiw

    unity_MDSWhX3wDG-uiw

    Joined:
    Sep 7, 2019
    Posts:
    2
    I'am writing a little game, and the core mechanic is that you click on an object, and then it changes from material. It worked, I could click a button and then change the material of an object. But now I added a mechanic that first checks what for material the object has. So first I check if the object has the correct material, if not so. There would't happen anything, but if it the current material of the object clicked on is correct, the object would change color. The if statement normally would come out correct, because I Debug.Log it and the material name is, "TreeStage1" what is correct.

    My Debug.Log outcome (it's alwo weird that I get 3 times the output for 1 click):
    TreeStage1 (Instance)
    UnityEngine.Debug:Log(Object)
    TreeSelect:Update() (at Assets/Scripts/TreeSelect.cs:34)


    Can anyone see the problem?
    Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class TreeSelect : MonoBehaviour
    5. {
    6.     public Material TreeStage1;
    7.     public Material Tree1Stage2;
    8.     public Material Tree2Stage2;
    9.  
    10.     public Renderer Object;
    11.  
    12.     private GameObject ObjectClicked;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         GameObject tree = GameObject.FindGameObjectsWithTag("Tree")[0];
    18.         ButtonScript buttonScript = tree.GetComponent<ButtonScript>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (Input.GetMouseButtonDown(0))
    24.         {
    25.             RaycastHit hit;
    26.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.  
    28.             if (Physics.Raycast(ray, out hit))
    29.             {
    30.                 ObjectClicked = hit.transform.gameObject;
    31.  
    32.                 if (hit.transform.tag == "Tree")
    33.                 {
    34.                     Debug.Log(ObjectClicked.GetComponent<Renderer>().material.name.ToString());
    35.                  
    36.                     if (ObjectClicked.GetComponent<Renderer>().material.name.ToString() == "TreeStage1")
    37.                     {
    38.                         ObjectClicked.GetComponent<Renderer>().material = Tree1Stage2;
    39.                         Debug.Log("Clicked-Apple");
    40.                     }
    41.                 }
    42.             }
    43.         }
    44.     }
    45. }
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    837
    Whenever Renderer.material is accessed the first time a clone of the material is created, thus making your name "TreeStage1 (Instance)" not the same as "TreeStage1"
    If you don't intend to modify material properties you should use Renderer.sharedMaterial so unity won't instantiate a clone for you.
    Then you should be able to just check materiald for equality (the namecheck is not needed)
    As for the multiple logs, you probably have more than one of these scripts in your scene.
     
    unity_MDSWhX3wDG-uiw likes this.
  3. unity_MDSWhX3wDG-uiw

    unity_MDSWhX3wDG-uiw

    Joined:
    Sep 7, 2019
    Posts:
    2
    Thank You so much,
    Now it works!