Changing Emission Color in script

This the glow code when called makes the item glow(). But that is a one shot. I figured the Update should turn off the glow at the next update, but that is not happening.

using UnityEngine;
using System.Collections;

public class StoneScript : MonoBehaviour
{
    Color brighter;
    Color greenGrey;
    Material myMaterial;
    bool bright = false;

    // Use this for initialization
    void Start()
    {
        ColorUtility.TryParseHtmlString("#3E594CFF", out greenGrey);
        brighter = greenGrey * Mathf.LinearToGammaSpace(0.8f);
        myMaterial = GetComponent<Renderer>().material;
    }

    public void glow()
    {
        myMaterial.SetColor("_EmissionColor", brighter);
        bright = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (bright)
        {
            bright = false;
            myMaterial.SetColor("_EmissionColor", greenGrey);
        }
    }
}

Hmm... I'm using a script I found for an enemy patrol behavior that relies on NavMeshAgent to work, and it seems that it is NavMeshAgent that causes the problem. Maybe my patrol script is fucky? Thanks...

2 Answers

2

Seems like your code is correct. Where are you calling glow()? Maybe you are calling glow() every frame so bright keeps getting set to true?

Is the “if(bright)” conditional happening? Try putting a print statement in there to check.

You said "the AI spawns in". How exactly are you spawning something without Instantiate?

I have tried a couple of things (including what you said).

  1. I take out the if (bright) all twelve stones (connected to stoneScript) go bright immediately.

  2. Glow is being called by a rotating object that points at one of the 12 items at a time. If I cut the framerate to 1fps, I can watch each one go one at a time, but then never turn off.

  3. The object calling glow is doing the following:

    void Update()
    {
    RaycastHit hit;
    Vector3 origin = transform.position;
    Vector3 fwd = transform.TransformDirection(Vector3.forward);
    origin += fwd * (carLength / 2); // move sensor to front bumper
    bool hitTest = Physics.Raycast(origin, fwd, out hit);
    Debug.DrawRay(origin, fwd * hit.distance, Color.cyan, 0, true);
    if (!Dispatcher.me.ready) return;
    if (!Dispatcher.me.work) return;
    if (hitTest)
    {
    GameObject target = hit.collider.gameObject;
    StoneScript stone = target.GetComponent();
    results.hitDistance = hit.distance;
    results.hitID = target.name;
    stone.glow();
    }
    }

@akiva42 (Sheli: Yechezkal Shimon)

@DrYSG Sorry, but have you found a solution it?