problem with instantiated clones not running OnEnterTrigger / OnexitTrigger script?
Hello, I have been looking around the forums about a problem I'm having with instantiating a prefab with a script attached. I'm pretty sure it has to do with the way I'm structuring my code either in the class or having my class try to handle too much script into one class type.
I have two objects, one that can be captured and the other that is instantiated. When capturing starts (capture loading bar) the player has entered the trigger and stays within this trigger. The object counts down 5f and instantiates the GUI prefab that displays this to the user. The issue is that the cloned loading bar never fills the bar unless all the loading bar code is inside the Update function. I have the same exact prefab within my scene sitting to the side and it will fill the loading bar with all loading bar code sitting in my OnTriggerStay function but when it is cloned / instantiated I have no action taking place.
When you plan to make a GUI element or any object instantiate itself, what is the best practice for making sure the script attached is running when instantiated? Especially when you have code outside of the Update or FixedUpdate function?
I found that if I place the code I have for the loading bar in the Update function it runs, is this where my code should be and I just create a if (trigger = true) fill bar / else detroy object to check to see if the loading bar should be active? I would like to keep the code within my OnTriggerEnter Function but am I doing something wrong in the code?
this is the code that works only for the prefab that is currently residing in the scene
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Capture : MonoBehaviour {
float countdown = 5f;
public Transform LoadingBar;
//public Transform prefab;
public Transform prefab2;
public GUI guiprefab;
public Transform position;
[SerializeField] private float currentAmmount;
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter (Collider other)
{
Transform prefab;
prefab = Instantiate(prefab2, position.position, position.rotation) as Transform;
}
void OnTriggerStay (Collider other)
{
if (other.attachedRigidbody)
{
//Instantiate (prefab, position.position, position.rotation);
currentAmmount += countdown * Time.deltaTime * 8;
countdown -= Time.deltaTime;
Debug.Log(currentAmmount);
LoadingBar.GetComponent<Image>().fillAmount = currentAmmount / 100;
if (countdown <= 0.0f)
{
GetComponent<Renderer>().material.color = Color.red;
}
}
}
void OnTriggerExit (Collider other)
{
Color team = GetComponent<Renderer>().material.color;
if (team == Color.red)
{
countdown = 5f;
}
else
{
countdown = 5f;
GetComponent<Renderer>().material.color = Color.blue;
}
}
}
if I move the code for the loading bar within the update function it works for the instantiated object, but like I mentioned I would like to find a way to keep it within the OnTriggerStay.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Capture : MonoBehaviour {
float countdown = 5f;
public Transform LoadingBar;
//public Transform prefab;
public Transform prefab2;
public GUI guiprefab;
public Transform position;
[SerializeField] private float currentAmmount;
// Update is called once per frame
void Update()
{
currentAmmount += countdown * Time.deltaTime * 8;
countdown -= Time.deltaTime;
Debug.Log(currentAmmount);
LoadingBar.GetComponent<Image>().fillAmount = currentAmmount / 100;
}
void OnTriggerEnter (Collider other)
{
Transform prefab;
prefab = Instantiate(prefab2, position.position, position.rotation) as Transform;
}
void OnTriggerStay (Collider other)
{
if (other.attachedRigidbody)
{
countdown -= Time.deltaTime;
if (countdown <= 0.0f)
{
GetComponent<Renderer>().material.color = Color.red;
}
}
}
void OnTriggerExit (Collider other)
{
Color team = GetComponent<Renderer>().material.color;
if (team == Color.red)
{
countdown = 5f;
}
else
{
countdown = 5f;
GetComponent<Renderer>().material.color = Color.blue;
}
}
}
I appreciate any and all feedback and would love to know what I am doing wrong with my approach. One thing to note is that I have this script attached to both the object being captured and the GUI element being instantiated. Is this the right approach?
one more thought, is it having issues because the instantiated object has not detected an OnTriggerStay event so therefore it does not count up?
Thank you!