• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xXSilverswordXx · Jun 22, 2017 at 09:55 AM · 2dcollisionvariablesstoring

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#

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

181 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d box collider not working (have rigid body) 0 Answers

2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer

Get tile from trigger 0 Answers

onCollisionEnter dosent work when player and enemy go to the same direction,onCollisionEnter2D work only in 1 side 0 Answers

On collision x or y 2d c# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges