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

how to change the player material by clicking the prefab?

Discussion in 'Scripting' started by ArthurUnity2, Sep 1, 2019.

  1. ArthurUnity2

    ArthurUnity2

    Joined:
    Apr 19, 2017
    Posts:
    6
    Firstly,i use to this script to create ScriptableObject--ShopItem

    Code (CSharp):
    1.  
    2. [CreateAssetMenu(menuName = "Shop/Shop Item")]
    3. public class ShopItem : ScriptableObject
    4. {
    5.     public string itemName;
    6.  
    7.  
    8.     public Material material;
    9.     public Mesh mesh;
    10.     public Sprite image;
    11.     public Button m;
    12. }

    after that,i use this script to create a skin shop list and create 6 sphere with 6 different materials


    Code (CSharp):
    1. public void Start()
    2.     {
    3.         ABCShop();
    4.    
    5.     }
    6.     public void ABCShop()
    7.     {
    8.         for (int i = 0; i < shopItem.Length; i++)
    9.         {
    10.             ShopItem si = shopItem[i];
    11.             GameObject itemObject = Instantiate(shopItemPrefab, shopContainer);
    12.        
    13.             itemObject.GetComponent<Button>().onClick.AddListener(() => OnButtonClick(si));
    14.          
    15.             itemObject.GetComponent<Renderer>().material = si.material;
    16.             itemObject.GetComponent<MeshFilter>().sharedMesh = si.mesh;
    17.          
    18.         }
    19.     }
    20.  
    21.   public void OnButtonClick(ShopItem item)
    22.     {
    23.      
    24.  
    25.     }
    26.  
    now,I create 6 sphere with 6 different materials
    what should i write in OnButtonClick function to change the material of my player?
    For example,
    when Sphere A have material A,when i click on Sphere A,the material of my player will change to material A
    when Sphere B have material B,when i click on Sphere B,the material of my player will change to material B
    i wrote this script,but not working :confused:
    Code (CSharp):
    1.    public void OnButtonClick(ShopItem item)
    2.     {
    3.      
    4.         GetComponent<Renderer>().material = M.Newskin;
    5.        Debug.Log(item.name);
    6.     }
     
  2. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    Looking over your scripts, I would guess you need a link the player doing the clicking.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.             RaycastHit hit;
    7.             if (Physics.Raycast(ray, out hit))
    8.             {
    9.                 hit.collider.GetComponent<Renderer>().material = M.Newskin;
    10.                 // or hit.collider.GetComponent<Renderer>().material = GetComponent<Material>();
    11.             }
    12.         }
    13.     }
     
  3. ArthurUnity2

    ArthurUnity2

    Joined:
    Apr 19, 2017
    Posts:
    6
    Can i just write in this?pr is assigned to shopitem, player is assigned to player

    i put the script in unity inspectror's button and call change(),why it doesnt work?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Events;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class SwitchSkin : MonoBehaviour
    7. {
    8.     public GameObject Player;
    9.     public GameObject pr;
    10.  
    11.     void Update()
    12.     {
    13.        
    14.     }
    15.  
    16.     public void Change()
    17.     {
    18.    
    19.             pr.GetComponent<Renderer>().material = Player.transform.GetChild(3).GetComponent<Material>();
    20.             // or hit.collider.GetComponent<Renderer>().material = GetComponent<Material>();
    21.        
    22.     }
     
  4. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    Firstly if you are changing the player you might want to flip your code.

    Code (CSharp):
    1. Player.transform.GetChild(3).GetComponent<Material>()=pr.GetComponent<Renderer>().material;
    Secondly, It is hard to figure out how the entire system works, but maybe the renderer is further down and you could try

    Code (CSharp):
    1. pr.GetComponentInChildren<Renderer>().material
    Try to Debug.Log(material.name), for the pr and the same for the player. The name should tell you if you have the right object, sometimes it is pointing to null.