How can I use the same script on multiple objects without conflicting variables.

Hi, I’ve made a script that attaches an array of materials to an object. When you click the object, the next material in the array shows up. You can add the script to multiple objects, each with a unique array of materials. Problem is I’ve used an integer iteration to cycle through the arrays. So when I click on any object it does change to the next material, but it changes to the next material on all the objects.

See code below:

 public class MaterialChangeOnClick : MonoBehaviour
    {
        public Material[] material;
        private int nextMat = 1;
        Renderer rend;
    
        void Start()
        {
            rend = GetComponent<Renderer>();
            rend.enabled = true;
            rend.sharedMaterial = material[0];
    
        }
    
        void Update()
        {             
            if (Input.GetMouseButtonDown(0))
            {
                int layerMask = 1 << 10;
                layerMask = ~layerMask;
    
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
    
                if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
                {
                    GetComponent<Renderer>().material = material[nextMat];
    
                    Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
                    Debug.Log("Did Hit");
    
                    nextMat = nextMat + 1;
                                 
                }
    
                if (nextMat == material.Length)
                {
                    nextMat = 0;
                }
    
            }
        }
    }

Here’s the script I cobbled together from a few different tutorials and ad libs:

public class MaterialChangeOnClick : MonoBehaviour
{
    public Material[] material;
    private int nextMat = 1;
    Renderer rend;

    void Start()
    {
        rend = GetComponent<Renderer>();
        rend.enabled = true;
        rend.sharedMaterial = material[0];

    }

    void Update()
    {             
        if (Input.GetMouseButtonDown(0))
        {
            int layerMask = 1 << 10;
            layerMask = ~layerMask;

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
            {
                GetComponent<Renderer>().material = material[nextMat];

                Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
                Debug.Log("Did Hit");

                nextMat = nextMat + 1;
                             
            }

            if (nextMat == material.Length)
            {
                nextMat = 0;
            }

        }
    }
}

Hi @admmalan, the issue in your script is how you do the check condition for RayCasting. Currently all your scripts run and all return true for the ray hitting. That in turn makes them all switch. Nothing to do with the actual array itself. Read the notes below, but also have a look at how the raycast function was done. Because you create a Ray but never use it. Which is your script is failing.


Even if you do fix this issue, you are still doing it in a very computationally expensive way. Because say you have 100 objects, every time you click all 100 objects will need to do a raycast and check for the condition. This will quickly get out of control and just kill performance. Below I have written a solution in two scripts. These scripts will fix the issue you are having AND optimise the process so only one function is executed on every click.


The idea behind it is you have two types of scripts.

  • ObjectOnClickEvents : This will control the click events, attach this to one object only (like camera or player)
  • ObjectMaterial : This will go on all the objects you want to have materials on. Basically it will replace the script you have now.

ObjectMaterial

	// Object Material Script
	// Attach to objects
	public class ObjectMaterial : MonoBehaviour {
		
		public Material[] material;
		Render rend;
		
		void Start()
		{
			rend = GetComponent<Renderer>();
			rend.enabled = true;
			rend.sharedMaterial = material[0]; 
		}
		
		public void NextMaterial() {
			// Change to the next material
			rend.material = material[nextMat];
			
			// Increase the material
			nextMat = nextMat + 1;
			
			// Reset if neccecary
			if (nextMat == material.Length)
				nextMat = 0;
		}
	}

ObjectOnClickEvents

	// Controller Script
	// Attach to Player/Camera
	public class ObjectOnClickEvents : MonoBehaviour {
		void Update()
		{             
			// Handle on click events for materials changing materials
			HandleMaterialChangeOnClick();
			
			// Other on click events
			/* ... */
		}
		
		void HandleMaterialChangeOnClick () {
			if (Input.GetMouseButtonDown(0)){
				int layerMask = 1 << 10;
				layerMask = ~layerMask;
	 
				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				RaycastHit hit;
				
				if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)){
					// Check to see if the object has a ObjectMaterial script
					var objMat = hit.transform.GetComponent<ObjectMaterial>();
					
					// Change the material
					if (objMat != null)
						objMat.NextMaterial();
				}
			}
		}
	}

Thanks for the reply! I think I understand what you’re suggesting and it makes sense. I’ll try implement it and see if I can get it to work.

edit: yep it worked great. and it did improve performance quite a notably. Thank you!