- Home /
Repawning pickup, disable renderer and collider
So I'm trying to expand a bit on the Roll a ball tutorial, and respawning my pickups after 10 seconds. To do this I have this script that they all follow:
using UnityEngine;
public class pickupBehaviour : MonoBehaviour {
private float despawnTime;
// Update is called once per frame
void Update()
{
if (Time.time - despawnTime > 10)
{
GetComponent<Renderer>().enabled = true;
GetComponent<Collider>().enabled = true;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
GetComponent<Renderer>().enabled = false;
GetComponent<Collider>().enabled = false;
despawnTime = Time.time;
}
}
}
In the beginning I was just using
this.enabled = false / true
But then they wouldn't respawn at all since it would untick everything in the inspector, including the script itself. So I searched a bit and found out that I could get to the renderer and collider and decided to use that. But even so, when I roll into a pickup, it disables the renderer and the collider, but also the object itself. The script box is still ticked in, but it's not respawning.
Answer by madks13 · Aug 10, 2018 at 10:11 AM
Umm, if the box is ticked, i guess your if checks are wrong. Check to see what values are actualy provided for Time.time and despawnTime. I would also suggest to replace the "10" by a variable, and make it public so you can change it in the editor if needed.
Your answer
Follow this Question
Related Questions
I need to puck up object using a key, but something is wrong with code 1 Answer
Disabling collider according to color? 2 Answers
Get All Renderers inside of GameObejct 0 Answers
Check if trigger collider is touching other trigger collider 2 Answers
Does anyone know how to activate different scripts OnTrigger? 1 Answer