Is this possible? OnTriggerEnter with a delay that affects the color of an gameObject.
Hello,
I'm not sure if I understand what's going wrong with my script or if what I'm trying to do is even possible. Sorry for the messy script I was trying a few things i thought would fix it.
Basically I want the gameObject that is detected during OnTriggerEnter to change color after a certain amount of time. However and I believe it has something to do with Collider part of OnTriggerEnter; the script errors out saying line 39 in fixedupdate has null reference. meatCollision.gameObject.GetComponent().material.color = mycolor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrillFunction : MonoBehaviour
{
public Color mycolor;
private bool trigger;
public float cookTime;
private Collider meatCollision;
public void Start()
{
meatCollision = null;
}
public void OnTriggerEnter(Collider meatCollision)
{
if (meatCollision.CompareTag("RawMeat"))
{
Debug.Log("This is Raw meat" + meatCollision.name);
StartCoroutine(Example());
}
}
private void FixedUpdate()
{
if (trigger == true)
{
Debug.Log("It became true for fixed update");
meatCollision.gameObject.GetComponent<Renderer>().material.color = mycolor;
}
}
IEnumerator Example()
{
trigger = false;
yield return new WaitForSeconds(cookTime);
trigger = true;
Debug.Log(trigger);
}
}
I believe my issue revolves around OnTriggerEnter collider not being available outside the function because the color changes when the code is inside the function but never outside. Any help would be greatly appreciated. Sorry if I missed any information
Thanks.
and why FixedUpdate? no physics are involved and I thought fixedUpdate was more "expensive" than a simple Update. and I am wondering what you drag into the public variable of "mycolor". sorry , more questions than answers. I do a few color changes and they don't seem as efficient as your script. Them again, $$anonymous$$e work, but.....................bad code
Answer by dakshesh1010 · Apr 26, 2018 at 06:23 AM
The private meatCollision variable is always null in your case. When you call that in FixedUpdate, it still remains null. The meatCollision inside the OnTriggerEnter is just the argument you are passing, which is local to that function. You might want to assign the global meatCollision at this point. I would recommend changing the argument's name in OnTriggerEnter and assign meatCollision with the value of the argument in case it has the desired tag. Your code should look something like this:
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("RawMeat"))
{
//Debug.Log("This is Raw meat" + meatCollision.name);
meatCollision = other;
StartCoroutine(Example());
}
}
Now when in FixedUpdate, when you change it's color, it will not throw Null exception.
Awesome answer. $$anonymous$$akes much more sense as I was thinking once it collided, 'other' was now set as the object it collided with. Thank you for your answer. I was overthinking it for sure.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161535im_/https://answers.unity.com/themes/thub/images/avi.jpg)