Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing material doesnt work

Discussion in 'AR/VR (XR) Discussion' started by e1e1e1, Sep 2, 2019.

  1. e1e1e1

    e1e1e1

    Joined:
    Jan 11, 2019
    Posts:
    58
    I made radial material changer.
    first, empty object of the material changer with child collidr and child sprite
    using the following script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MaterialChanger : MonoBehaviour
    6. {
    7.     public RadialMenu menuPrefab;
    8.     public Renderer ObjectToChange;
    9.  
    10.     [HideInInspector]
    11.     public bool PointerOverButton, PointerOverMenu;
    12.  
    13.     [System.Serializable]
    14.     public class Materialinfo
    15.     {
    16.         public string title;
    17.         public Material material;
    18.     }
    19.  
    20.     public Materialinfo[] materials;
    21.  
    22.     private RadialMenu currentMenu;
    23.     private Transform childCollider;
    24.  
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         childCollider = transform.GetChild(0);
    30.     }
    31.  
    32.     public void OnPointerEnter()
    33.     {
    34.         PointerOverMenu = true;
    35.         //spawn a menu
    36.         if (currentMenu == null)
    37.         {
    38.             currentMenu = Instantiate(menuPrefab) as RadialMenu;
    39.             currentMenu.transform.position = transform.position;
    40.             childCollider.localScale = new Vector3(1.5f, 1.5f, 1.5f);
    41.  
    42.             //spawn buttons
    43.             currentMenu.SpawnButtons(this);
    44.  
    45.         }
    46.     }
    47.  
    48.     public void OnPointerExit()
    49.     {
    50.         PointerOverMenu = false;
    51.         StartCoroutine(DestroyMenu());
    52.     }
    53.  
    54.     public IEnumerator DestroyMenu()
    55.     {
    56.         yield return new WaitForEndOfFrame();
    57.         if (PointerOverButton == false && PointerOverMenu == false)
    58.         {
    59.             Destroy(currentMenu.gameObject);
    60.             childCollider.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    61.         }
    62.  
    63.     }
    64.  
    65. }
    66.  
    using canvas operate as the radial menu with child panel. the panel have child image operate as background and child image as button, using the following script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RadialMenu : MonoBehaviour
    6. {
    7.     public RadialButton buttonPrefab;
    8.     public bool autoSpaceButton;
    9.     public int numberOfButtons = 6;
    10.     public float buttonSize = 50f;
    11.     public float distanceFromCenter = 50f;
    12.  
    13.     private RectTransform newButtonrect;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     public void SpawnButtons(MaterialChanger obj)
    22.     {
    23.         for (int i = 0; i < obj.materials.Length; i++)
    24.         {
    25.             RadialButton newButton = Instantiate(buttonPrefab) as RadialButton;
    26.             newButton.transform.SetParent(transform.GetChild(0));
    27.             newButton.transform.localScale = Vector3.one;
    28.             newButtonrect = newButton.gameObject.GetComponent<RectTransform>();
    29.             newButtonrect.sizeDelta = new Vector2(buttonSize, buttonSize);
    30.             newButton.title = obj.materials[i].title;
    31.             newButton.material = obj.materials[i].material;
    32.             newButton.obj = obj;
    33.  
    34.             float theta = (2 * Mathf.PI / obj.materials.Length) * i;
    35.             float xPos = Mathf.Sin(theta);
    36.             float yPos = Mathf.Cos(theta);
    37.             newButton.transform.localPosition = new Vector3(xPos, yPos, 0f) * distanceFromCenter;
    38.        
    39.         }
    40.     }
    41. }
    and the button script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RadialButton : MonoBehaviour
    6. {
    7.     [HideInInspector]
    8.     public string title;
    9.     [HideInInspector]
    10.     public Material material;
    11.     [HideInInspector]
    12.     public MaterialChanger obj;
    13.  
    14.     private MeshRenderer childRenderer;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         childRenderer = transform.GetChild(0).gameObject.GetComponent<MeshRenderer>();
    20.         childRenderer.material = material;
    21.     }
    22.  
    23.     public void OnPointerEnter()
    24.     {
    25.         obj.PointerOverButton = true;
    26.         obj.ObjectToChange.material = material;
    27.     }
    28.     public void OnPointerExit()
    29.     {
    30.         obj.PointerOverButton = false;
    31.         StartCoroutine(obj.DestroyMenu());
    32.     }
    33.  
    34.     public void OnPointerDown()
    35.     {
    36.         obj.ObjectToChange.material = material;
    37.     }
    38.    
    39.  
    40.  
    41. }
    I made a new prefabe folder and drag the cnvas and the button to this folder and put them in their place in the scripts. everything technicly working fine, but its just doesnt changeing the material of the mesh I choosed. what can it be?