Issues with Materials change Via script

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Marked : MonoBehaviour
{
    public bool IamMarkedForSelection=false;
    public Material[] origiMat;

    public GameObject WallModel;



    // Start is called before the first frame update
    void Start()
    {
        origiMat = transform.parent.Find("WallModel").gameObject.GetComponent<MeshRenderer>().materials;
        WallModel = transform.parent.Find("WallModel").gameObject;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (IamMarkedForSelection == false && !Input.GetMouseButtonDown(0))
        {
            WallModel.GetComponent<MeshRenderer>().materials = origiMat;
        }
    }

}

Select Object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelectObject : MonoBehaviour
{
    public GameObject targetedObjName;
    public GameObject previousGameObject;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    private void FixedUpdate()
    {
        getTargObject();


    }
    // Update is called once per frame
    void Update()
    {
       
       
    }

    public void getTargObject()
    {

        var cam = GetComponent<Camera>();
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 1000))
        {
            ////+
            //if we target wall



            var nextGameObject = hit.collider.gameObject;
            if (nextGameObject != targetedObjName)
            {
                previousGameObject = targetedObjName;
                targetedObjName = nextGameObject;
                if (previousGameObject!=null && previousGameObject.tag == "WallSide")
                {
                    previousGameObject.GetComponent<Marked>().IamMarkedForSelection = false; 
                }
                if(targetedObjName.tag=="WallSide" && targetedObjName != null)
                {
                    targetedObjName.GetComponent<Marked>().IamMarkedForSelection = true;
                }


            }
        }

    }
}

PaintWall

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PaintWalls : MonoBehaviour
{
    //from here we will grab the components to track Mouse Pos
    public SelectObject selectObject;
    public GameObject wallTobePainted;

    [SerializeField] public Material materialToPaint;

    [SerializeField] public bool paintOn = false;
    public bool paintNextOne = false;


    public Material[] arrOrigMat;




    // Start is called before the first frame update
    void Start()
    {
        selectObject = GetComponent<SelectObject>();
        //arrOrigMat = selectObject.targetedObjName.transform.parent.Find("WallModel").gameObject.GetComponent<MeshRenderer>().materials;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (paintOn)
        {
            var sideOfwall = selectObject.targetedObjName;
            if (sideOfwall.CompareTag("WallSide"))
            {
               
                wallTobePainted = sideOfwall.transform.parent.Find("WallModel").gameObject;
                           

               
                if (sideOfwall.name == "OneSide")
                {
                    //Preview
                    if (selectObject.targetedObjName.GetComponent<Marked>().IamMarkedForSelection && !paintNextOne)
                    {
                        var arrMat = new Material[] { wallTobePainted.GetComponent<MeshRenderer>().materials[0], materialToPaint };
                        wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
                        Debug.Log("StartPreview" + sideOfwall.GetInstanceID());
                    }
                    //Change Element 1
                    if (Input.GetMouseButtonDown(0) || paintNextOne)
                    {
                       
                        paintNextOne = true;
                        var arrMat = new Material[] { wallTobePainted.GetComponent<MeshRenderer>().materials[0], materialToPaint };
                        wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
                        sideOfwall.GetComponent<Marked>().origiMat = arrMat;
                       
                    }
    
                    if (Input.GetMouseButtonUp(0))
                    {
                        paintNextOne = false;
                    }

                }
                if (sideOfwall.name == "TwoSide")
                {
                    //Preview
                    if (selectObject.targetedObjName.GetComponent<Marked>().IamMarkedForSelection && !paintNextOne)
                    {
                        var arrMat = new Material[] { materialToPaint, wallTobePainted.GetComponent<MeshRenderer>().materials[1] };
                        wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
                        Debug.Log("StartPreview" + sideOfwall.GetInstanceID());
                    }


                    //Change Element 0
                    if (Input.GetMouseButtonDown(0) || paintNextOne)
                    {
                      
                        paintNextOne = true;
                        var arrMat = new Material[] { materialToPaint, wallTobePainted.GetComponent<MeshRenderer>().materials[1] };
                        wallTobePainted.GetComponent<MeshRenderer>().materials = arrMat;
                        sideOfwall.GetComponent<Marked>().origiMat = arrMat;
                       
                    }
                    if (Input.GetMouseButtonUp(0))
                    {
                        paintNextOne = false;
                    }


                }
            }
            if (Input.GetMouseButtonUp(0))
            {
                paintNextOne = false;
            }


        }
        if (Input.GetMouseButtonUp(0))
        {
            paintNextOne = false;
        }
       
    }

    public void activatePaint()
    {
        if (paintOn == false)
        {
            paintOn = true;
        }
        else
        {
            paintOn = false;
        }

    }

    public void OnMouseOver()
    {

    }

}

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 :

  • WallModel.GetComponent().materials = origiMat;