- Home /
The question is answered, right answer was accepted
How do I destroy a game object my character hit ONLY if a condition is active?
I need to do two things.
1) have a pickup object that triggers a particle system on my character for x amount of time.
2) have certain game objects able to be destroyed ONLY IF that particle system is active on my character otherwise the character dies when colliding with that same object.
Answer by StevenUnu · Sep 09, 2017 at 07:22 PM
For the pickup object:
• Make it a trigger
• Code:
public void OnTriggerEnter(Collider col) {
if(col.gameObject.name == "Character")
/* I don't know much about the light system so */
// do light magic here
lightMagic = true ; // have a boolean to know when the character is lit.
}
For the other part :
void OnCollisionEnter(Collider col) {
if(col.gameObejct.name == " Character")
if(lightMagic)
Destroy(col.gameObject) ;
else Destroy(character) ;
}
Answer by RyanAtEvno · Sep 11, 2017 at 10:05 PM
So, I used a bit of what you recommended and a bit of stuff I just experimented with. Sharing for others.
This script is attached to my Player. I made my particle system attachable to the script via public ParticleSystem. On trigger, the pickup item is destroyed AND the particle system is played for 5 seconds (by unchecking "Looping" under particle system Inspector window and setting duration to 5 seconds)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirePickup : MonoBehaviour {
public ParticleSystem FirePickUp;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up Fire"))
{
other.gameObject.SetActive (false);
FirePickUp.Play ();
}
}
}
Next, also attached to the Player, this script destroys the "IceBarrier" object when Player collides on Trigger.
public class IceBarrierDestroy : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("BarrierIce"))
{
other.gameObject.SetActive (false);
}
}
}
The only remaining objective for this thread is, when Player collides with the object, the object (Icebarrier) is only destroyed IF the particle system (FlamePickUp) is active (5 sec duration remember) or ELSE Player is destroyed (by still active IceBarrier)
I added a public boolean for the particle system with public bool isPlaying; Added isPlaying = true; to the trigger on the pickup item. On the script that destroys the barrier object on trigger, I added an "&& gameObject.GetComponent().isPlaying" so it will only destroy object is particle is playing. Then I used the Invoke method in order to set the bool false after 5 seconds of the particle playing. Posting script below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirePickup : $$anonymous$$onoBehaviour {
public bool isPlaying;
public ParticleSystem FirePickUp;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up Fire"))
{
other.gameObject.SetActive (false);
isPlaying = true;
Invoke ("SetBoolBack", 5f);
FirePickUp.Play ();
}
}
private void SetBoolBack()
{
isPlaying = false;
}
}
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class IceBarrierDestroy : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("BarrierIce") && gameObject.GetComponent<FirePickup>().isPlaying)
{
other.gameObject.SetActive (false);
}
}
}