Question by
Naiveca · Apr 18, 2019 at 10:52 AM ·
objectcollidersactivationdeactivate
Activating an object after it's set as inactive?
I have an object with the following code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Disableheart : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
gameObject.SetActive(false);
}
}
After that I'm trying to turn it on with the following code, on a different object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnTrigger : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("nheart"))
other.gameObject.SetActive(true);
}
}
This doesn't work, but it works the other way by switching the "false" and "true" on the codes. Is there a way to get this to work?
I've also tried the following method but it doesn't work either.
Disabling the item at start.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Disableheart : MonoBehaviour
{
public GameObject heart;
private void Start()
{
heart = GameObject.Find("Naiveca_heart");
heart.SetActive(false);
}
}
Trying to activate it on collide.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnTrigger : MonoBehaviour
{
public GameObject heart;
void OnTriggerEnter(Collider other)
{
heart = GameObject.Find("Naiveca_heart");
heart.SetActive(true);
}
}
Still doesn't work :/
Why is enabling an item so hard after disabling it?
Comment
In your OnTrigger class, add "public DisableHeart dh;"
Drag the object to this field in the Editor.
You now have a reference to the object.
You can now enable/disable with dh.enabled(true);
Did this resolve your issue? If so, could you accept this answer as best answer please? Ta