0
\$\begingroup\$

I have this simple script, so the user touch one of the cubes all cubes with same material will change color.

One of this cubes must have the name of the color on it, only one, so the user touch the cube written RED and all cubes related to material RED will change color to RED.

I can't figure out how to write the name of the color in this unique cube. :( need help....

using UnityEngine;
using System.Collections;

public class allCubes : MonoBehaviour {

    public Color defaultColor;
    public Color selectedColor;
    private Material mat;


    void Start(){
        mat = renderer.sharedMaterial;
        mat.color = defaultColor;
    }

    void OnTouchDown () {
        mat.color = selectedColor;
    }

    void OnTouchUp () {
        mat.color = defaultColor;
    }

    void OnTouchStay () {
        mat.color = selectedColor;
    }

    void OnTouchExit () {
        mat.color = defaultColor;
    }
}
\$\endgroup\$
1
\$\begingroup\$

Your issue most likely is caused by your sentence in the start

mat = renderer.sharedMaterial;

Once you assign a shared material to objects you will change the material/appearance settings for each of the shared material instances. For more info check out the unity docs

To prevent this you should try using renderer.material instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.