- Home /
Make make gameObject invisible until collision with bullet
Hey trying to make to a script for making a wall in my game be set to deactivating it at the start of the game. And upon detection of a bullet passing through it will destroy the bullet & will make the wall become active and appear before disappearing and becoming inactive after specific amount of time.
Code below is what I have so far. How would I go about making it work?
public class AppearingWall : MonoBehaviour
{
// Use this for initialization
void Start()
{
gameObject.SetActive(false);
}
public void OnTriggerEnter(Collider col)
{
int countDown = 0;
if (col.tag == "Bullet")
{
gameObject.SetActive(true);
countDown++;
if (countDown > 5)
{
gameObject.SetActive(false);
}
}
}
}
Not sure but why not deactivate its renderer ins$$anonymous$$d gameobject?
I tried this before but when I ran the script with the box collider being a trigger and it meant that the player could still run through the wall as if it wasn't there. So trying this way now.
^^ this is the best option because disabling the GameObject will also disable it's physics components.
Answer by KMKxJOEY1 · Nov 01, 2014 at 01:32 AM
try something like this instead:
public class AppearingWall : MonoBehaviour
{
void Start()
{
renderer.enabled = false;
}
public void OnTriggerEnter(Collider col)
{
int countDown = 0;
if (col.tag == "Bullet")
{
renderer.enabled = true;
countDown++;
if (countDown > 5)
{
renderer.enabled = false;
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Check if the child of an object is active not working!!! 2 Answers
How do I destroy a gun bullet clone? 3 Answers
Script only works onetime 0 Answers