Search Unity

Question Issues with Materials change Via script

Discussion in 'Scripting' started by zmatiaki, Sep 2, 2020.

  1. zmatiaki

    zmatiaki

    Joined:
    Nov 21, 2018
    Posts:
    13
    Greeting to all,

    I am desparate for help cause I havent encoured similar error never before. So , please for your assistance.

    We have a wall that we want to change color. Below the parent object

    Wall(Parent)
    >Wall Model ( Gameobject with Two materials each working on each side of wall)
    >One Side ( Box Collider as trigger and "Marked" Script)
    >Two Side ( Box Collider as trigger and "Marked" Script)
    >Top Base(ignore)
    >Down Base(ignore)

    I have attached to camera two scripts one called "SelectObject" and other "paintWalls"
    The issues that I have is that I can change material on TwoSide wall but not in OneSide. Moreover, if I change the order of above childs it works reverse. I can not make it work for booth and I can not see why this happens!

    Thanks in advance, scripts below

    Marked

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Marked : MonoBehaviour
    6. {
    7.     public bool IamMarkedForSelection=false;
    8.     public Material[] origiMat;
    9.  
    10.     public GameObject WallModel;
    11.  
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         origiMat = transform.parent.Find("WallModel").gameObject.GetComponent<MeshRenderer>().materials;
    18.         WallModel = transform.parent.Find("WallModel").gameObject;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void FixedUpdate()
    23.     {
    24.         if (IamMarkedForSelection == false && !Input.GetMouseButtonDown(0))
    25.         {
    26.             WallModel.GetComponent<MeshRenderer>().materials = origiMat;
    27.         }
    28.     }
    29.  
    30. }
    31.  
    Select Object
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SelectObject : MonoBehaviour
    6. {
    7.     public GameObject targetedObjName;
    8.     public GameObject previousGameObject;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     private void FixedUpdate()
    17.     {
    18.         getTargObject();
    19.  
    20.  
    21.     }
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.        
    26.        
    27.     }
    28.  
    29.     public void getTargObject()
    30.     {
    31.  
    32.         var cam = GetComponent<Camera>();
    33.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    34.         RaycastHit hit;
    35.         if (Physics.Raycast(ray, out hit, 1000))
    36.         {
    37.             ////+
    38.             //if we target wall
    39.  
    40.  
    41.  
    42.             var nextGameObject = hit.collider.gameObject;
    43.             if (nextGameObject != targetedObjName)
    44.             {
    45.                 previousGameObject = targetedObjName;
    46.                 targetedObjName = nextGameObject;
    47.                 if (previousGameObject!=null && previousGameObject.tag == "WallSide")
    48.                 {
    49.                     previousGameObject.GetComponent<Marked>().IamMarkedForSelection = false;
    50.                 }
    51.                 if(targetedObjName.tag=="WallSide" && targetedObjName != null)
    52.                 {
    53.                     targetedObjName.GetComponent<Marked>().IamMarkedForSelection = true;
    54.                 }
    55.  
    56.  
    57.             }
    58.         }
    59.  
    60.     }
    61. }
    62.  
    PaintWall
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PaintWalls : MonoBehaviour
    6. {
    7.     //from here we will grab the components to track Mouse Pos
    8.     public SelectObject selectObject;
    9.     public GameObject wallTobePainted;
    10.  
    11.     [SerializeField] public Material materialToPaint;
    12.  
    13.     [SerializeField] public bool paintOn = false;
    14.     public bool paintNextOne = false;
    15.  
    16.  
    17.     public Material[] arrOrigMat;
    18.  
    19.  
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         selectObject = GetComponent<SelectObject>();
    26.         //arrOrigMat = selectObject.targetedObjName.transform.parent.Find("WallModel").gameObject.GetComponent<MeshRenderer>().materials;
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void FixedUpdate()
    31.     {
    32.         if (paintOn)
    33.         {
    34.             var sideOfwall = selectObject.targetedObjName;
    35.             if (sideOfwall.CompareTag("WallSide"))
    36.             {
    37.                
    38.                 wallTobePainted = sideOfwall.transform.parent.Find("WallModel").gameObject;
    39.                            
    40.  
    41.                
    42.                 if (sideOfwall.name == "OneSide")
    43.                 {
    44.                     //Preview
    45.                     if (selectObject.targetedObjName.GetComponent<Marked>().IamMarkedForSelection && !paintNextOne)
    46.                     {
    47.                         var arrMat = new Material[] { wallTobePainted.GetComponent<MeshRenderer>().materials[0], materialToPaint };
    48.                         wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
    49.                         Debug.Log("StartPreview" + sideOfwall.GetInstanceID());
    50.                     }
    51.                     //Change Element 1
    52.                     if (Input.GetMouseButtonDown(0) || paintNextOne)
    53.                     {
    54.                        
    55.                         paintNextOne = true;
    56.                         var arrMat = new Material[] { wallTobePainted.GetComponent<MeshRenderer>().materials[0], materialToPaint };
    57.                         wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
    58.                         sideOfwall.GetComponent<Marked>().origiMat = arrMat;
    59.                        
    60.                     }
    61.    
    62.                     if (Input.GetMouseButtonUp(0))
    63.                     {
    64.                         paintNextOne = false;
    65.                     }
    66.  
    67.                 }
    68.                 if (sideOfwall.name == "TwoSide")
    69.                 {
    70.                     //Preview
    71.                     if (selectObject.targetedObjName.GetComponent<Marked>().IamMarkedForSelection && !paintNextOne)
    72.                     {
    73.                         var arrMat = new Material[] { materialToPaint, wallTobePainted.GetComponent<MeshRenderer>().materials[1] };
    74.                         wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
    75.                         Debug.Log("StartPreview" + sideOfwall.GetInstanceID());
    76.                     }
    77.  
    78.  
    79.                     //Change Element 0
    80.                     if (Input.GetMouseButtonDown(0) || paintNextOne)
    81.                     {
    82.                      
    83.                         paintNextOne = true;
    84.                         var arrMat = new Material[] { materialToPaint, wallTobePainted.GetComponent<MeshRenderer>().materials[1] };
    85.                         wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
    86.                         sideOfwall.GetComponent<Marked>().origiMat = arrMat;
    87.                        
    88.                     }
    89.                     if (Input.GetMouseButtonUp(0))
    90.                     {
    91.                         paintNextOne = false;
    92.                     }
    93.  
    94.  
    95.                 }
    96.             }
    97.             if (Input.GetMouseButtonUp(0))
    98.             {
    99.                 paintNextOne = false;
    100.             }
    101.  
    102.  
    103.         }
    104.         if (Input.GetMouseButtonUp(0))
    105.         {
    106.             paintNextOne = false;
    107.         }
    108.        
    109.     }
    110.  
    111.     public void activatePaint()
    112.     {
    113.         if (paintOn == false)
    114.         {
    115.             paintOn = true;
    116.         }
    117.         else
    118.         {
    119.             paintOn = false;
    120.         }
    121.  
    122.     }
    123.  
    124.     public void OnMouseOver()
    125.     {
    126.  
    127.     }
    128.  
    129. }
     
  2. zmatiaki

    zmatiaki

    Joined:
    Nov 21, 2018
    Posts:
    13
    For Anyone that has issues with Materia changes dont forget you must assign the Array again in order to work . Specific on the above request here is the bug :
    1. WallModel.GetComponent<MeshRenderer>().materials = origiMat;