Storing variable collisions and editing variables in other files (2d)
In my game, the player needs to avoid missiles. (the project is in 2D, top down.)
This is my script:
using UnityEngine;
using System.Collections;
public class Missileexploder : MonoBehaviour {
private GameObject collidedobject;
public Sprite[] Explosions;
public GameObject healthbar;
public int health; // Note i dont know if this should be public as it is a variable accessed from another script
void Start () {
}
void Update () {
}
void blowup()
{
Destroy(collidedobject.GetComponent("Trail Renderer")); // Removes the trail on the missile that collided with the player
//collidedobject.GetComponent<SpriteRenderer>().sprite = Explosion[5];
// add a 0.1 second delay here
//collidedobject.GetComponent<SpriteRenderer>().sprite = Explosion[4];
// add a 0.1 second delay here
//collidedobject.GetComponent<SpriteRenderer>().sprite = Explosion[3];
// add a 0.2 second delay here
//collidedobject.GetComponent<SpriteRenderer>().sprite = Explosion[2];
// add a 0.2 second delay here
//collidedobject.GetComponent<SpriteRenderer>().sprite = Explosion[1];
// add a 0.2 second delay here
Destroy(collidedobject); // removes the missile that collided with the player from existance
}
void OnTriggerEnter2D(Collider2D other)
{
if (collision.gameObject.tag == "missile")
collidedobject = Collision2D.gameObject; // stores what object was hit and then uses that knowledge in the blowup script
health = GameObject.Find("Player").GetComponent<PlayerHealth>().Playerhealth;
blowup();
}
}
This script goes on the player. I want it so that if the missile hits the player, the missile shows an explosion effect (using sprite changes), before being removed. It should also damage the players health, but health is managed in another script.
Does my storing the colission.gameobject2d as a variable work also if there are any obvious errors can someone please point it out since it does not work.
Thanks everyone, i am quite new to unity c#
Your answer
Follow this Question
Related Questions
Destorying objects on collsion 1 Answer
OnCollisionEnter2D not detecting collision 1 Answer
Sprite Resizing in certain Positions 0 Answers
Rigidbody2D figits when pushed against an object. 0 Answers
Tilemap Collider problems 0 Answers