C# Activate Object on Collision with tag not working.
I am trying to have a de-activated object activate OnTriggerEnter. I'm not sure what I did wrong any help would be much appreciated!
using UnityEngine;
using System.Collections;
public class SnowLayer : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
gameObject.active = false;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Snowstorm")
{
gameObject.active = true;
}
}
}
Answer by Doireth · Mar 30, 2016 at 08:57 AM
The gameobject is being disabled in the update function, it will never trigger (OnTriggerEnter will never be called).
Thanks I thought that could be it but then how do I activate a de-active game object? I know I could transform it to a location or disable rendering but I need all of its scripts to stop running. Thanks again for your help!
I'm unclear which object you want to disable, is it the object the script you've provided attached to or the object that it collides with?
I'm also unclear as to what you're trying to achieve with disabling the object. If you want all the scripts to stop running and stop rendering etc. then why not just disable the gameobject fully.
In the "OnTriggerEnter" function you set the gameobject to being active (gameobject.active = true), why? I don't understand what this script is meant to do.
Sorry I should of explained it better. The gameobject is a layer of snow that I want to render only when a snowstorm collides with it. The snowstorm has the collider on it and moves around. The snow layer that I want to enable and disable has a script attached to it for slowing down player movement and decreasing temperature so when its not being rendered I dont want it still having those effects thats why I wanted to deactivate it. The reason I set it to active was that I wanted it to activate when the snowstorm has collided with it and then once the snowstorm leaves I want the layer of snow to deactivate.
If I understand what you're trying to do, try this:
using UnityEngine;
using System.Collections;
public class SnowLayer : $$anonymous$$onoBehaviour {
bool IsActive = false; // used to prevent excessive "enablings"
Renderer[] allRenderers;
$$anonymous$$onoBehaviour[] allBehaviours;
void Start() {
allRenderers = GetComponentsInChildren<Renderer>();
allBehaviours = GetComponentsInChildren<$$anonymous$$onoBehaviour>();
foreach (Renderer r in allRenderers) {
r.enabled = false;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
// don't disable this script
if (mb == this)
continue;
mb.enabled = false;
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag.Equals("Snowstorm")) {
if (!IsActive) {
EnableThisObject();
}
}
}
void EnableThisObject() {
IsActive = true;
foreach (Renderer r in allRenderers) {
r.enabled = true;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
mb.enabled = true;
}
}
}
It is still being disabled on run but doesn't seem to enable when the snowstorm collides with it.
@Doireth Sorry wouldn't let me respond to your comment! Its almost working thanks so much for all your help!! The snow appears when the snowstorm collides with it but when it leaves it doesn't disappear. I gave it a go but must be doing it wrong.
using UnityEngine;
using System.Collections;
public class SnowLayer : $$anonymous$$onoBehaviour {
bool IsActive = false; // used to prevent excessive "enablings"
Renderer[] allRenderers;
$$anonymous$$onoBehaviour[] allBehaviours;
void Start() {
allRenderers = GetComponentsInChildren<Renderer>();
allBehaviours = GetComponentsInChildren<$$anonymous$$onoBehaviour>();
foreach (Renderer r in allRenderers) {
r.enabled = false;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
// don't disable this script
if (mb == this)
continue;
mb.enabled = false;
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag.Equals("Snowstorm")) {
if (!IsActive) {
EnableThisObject();
}
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag.Equals("Snowstorm")) {
if (!IsActive) {
DisableThisObject();
}
}
}
void EnableThisObject() {
IsActive = true;
foreach (Renderer r in allRenderers) {
r.enabled = true;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
mb.enabled = true;
}
}
void DisableThisObject() {
IsActive = false;
foreach (Renderer r in allRenderers) {
r.enabled = false;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
mb.enabled = false;
}
}
}
($$anonymous$$oved above to a comment as it was posted as an answer)
You're also there. Try this:
using UnityEngine;
using System.Collections;
public class test : $$anonymous$$onoBehaviour {
bool IsActive = false; // used to prevent excessive "enablings"
Renderer[] allRenderers;
$$anonymous$$onoBehaviour[] allBehaviours;
void Start() {
allRenderers = GetComponentsInChildren<Renderer>();
allBehaviours = GetComponentsInChildren<$$anonymous$$onoBehaviour>();
foreach (Renderer r in allRenderers) {
r.enabled = false;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
// don't disable this script
if (mb == this)
continue;
mb.enabled = false;
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag.Equals("Snowstorm")) {
if (!IsActive) {
EnableThisObject();
}
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag.Equals("Snowstorm")) {
if (IsActive) {
DisableThisObject();
}
}
}
void EnableThisObject() {
IsActive = true;
foreach (Renderer r in allRenderers) {
r.enabled = true;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
mb.enabled = true;
}
}
void DisableThisObject() {
IsActive = false;
foreach (Renderer r in allRenderers) {
r.enabled = false;
}
foreach ($$anonymous$$onoBehaviour mb in allBehaviours) {
// don't disable this script
if (mb == this)
continue;
mb.enabled = false;
}
}
}
Your answer
Follow this Question
Related Questions
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers
C# OnTriggerEnter Issue 3 Answers
OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers
How to get the Collider other element Rigidbody? 0 Answers
Remove Door's collider if cube is in the trigger,Destroy Door's Collider if box is in the trigger 0 Answers