Code to identify an objects color and apply it to another not working ( no errors )

Hi there,

So for my project, an aspect is that I want to change 1 specific GameObjects color to match the one singled out. There aren’t any errors and I’m able to run the game. It just doesn’t change the color though. I’ve placed a standard Unity cube to see if it should work ( Since I thought the possibility of me using a sprite/imported object would be a problem ) but it still doesn’t do anything.

This all has to happen while the game is running, as this would be on an infinite loop if the player can keep progressing on.

My code is

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

public class ColorArray : MonoBehaviour
{
    public GameObject[] CirclePart;
    public int activeCircle;
    public GameObject Arrow;
    public GameObject ColorGrabber;


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

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

    }


    void NewCircle()
    {
        activeCircle = Random.Range(0, CirclePart.Length);
        ColorGrabber = CirclePart[activeCircle];
        Color thisColor = ColorGrabber.GetComponent<SpriteRenderer>().material.GetColor("_Color");
        GetComponent<SpriteRenderer>().material.color = thisColor;
        Arrow.GetComponent<SpriteRenderer>().material.SetColor("_Color", thisColor);

    }




}

If you’ve got an idea as to why this is, your help is very appreciated.

Steps:

  • copy the material out of the Renderer and assign it to a local variable
  • change the material properties you want to change
  • assign it back to the renderer

Hey, so I didn’t clarify too well. I need to be doing this all while the games running, as it’ll be happening on an infinite loop if you can keep progressing. It’s so that the player will easily be able to identify the GameObject that is the goal. Unless I’m not understanding the steps clearly.

That’s what this is for.

You need to:

  1. make a copy of the material
  2. change that copy
  3. assign the material back to the renderer

Not sure how it can be said clearer.

Okay gotcha, will do! Currently I’m not even able to duplicate the material and can’t find it within the project tab. Will figure it out though. Cheers for the responses

Material temp = renderer.material;

temp.color = .... whatever

renderer.material = temp;

That’s it.

You a blessing my man, thank you!