Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 andrew23890 · May 27, 2016 at 03:48 PM · c#unity 5oncollisionenter

Unity2d trying to make an enemy react to an attack with a flash. Why isn't this code working?

My first problem is getting this enemy to react. I'm sure I've gotta be doing something simple wrong. The if statement in the oncollision2d method is never being called. The information in that if statement isn't being printed.

The name of the asset that collides with the enemy is AttackSpellPrefab. There's a separate attack script that handles instantiating the attack and sending it along a path. in the hierarchy, when the attack instantiates, an item called AttackSpellPrefab(Clone) is created. Is that the correct name to reference in the above mentioned if statement? that script is attached to the player. the attack spell prefab is then dragged into a slot for a rigidbody2d on the player.

If it makes a difference on where I need to write the script, I would like to be able to switch between different attacks, so I think I would need to create a "spellbook" object that holds the master list of abilities (or just two to get the code going) and reference it from there, with the game manager script checking to see which spells the player has access to. when the player switches attacks, an object (the spellbook object?) should assign the correct image and rigidbody and collider to the player.

I can provide all of my code if you need more than just this class's code to help.

using UnityEngine; using System.Collections;

 public class EnemyScript : MonoBehaviour {
 
     Rigidbody2D myBody;
     [SerializeField]
     float hitPoints = 10; //tweakable number of hit points to initialize with
     float currentHitPoints; //HP number to work with in script
     Transform myTransform;
     //[SerializeField]
   //  Sprite splatSprite; //sprite to indicate death
    // [SerializeField]
     //Sprite liveSprite; //sprite to indicate not dead.
     SpriteRenderer mySprite;
 
 
     // Use this for initialization
     void Start() {
 
         myBody = GetComponent<Rigidbody2D>();
         currentHitPoints = hitPoints;
 
     }
 
    
     public void OnCollisionEnter2d(Collision2D other)
     {
         if (other.transform.name == "AttackSpellPrefab(Clone)")
         {
                 currentHitPoints--;
             FlashIt();
             print("I've been hit!");
             print(currentHitPoints);
 
         }
     }
     IEnumerator FlashIt()
     {
         GetComponent<SpriteRenderer>().enabled = false;
         yield return new WaitForSeconds(.5f);
         GetComponent<SpriteRenderer>().enabled = true;
     }
 
     public void HealthCheck()
     {
         if (currentHitPoints < 1)
         {
             //   mySprite.sprite = splatsprite
             print("I Die");
         }
     }
 
         // Update is called once per frame
         void Update () {
         HealthCheck();
         }
     } 
 
Comment
Add comment · Show 10
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
avatar image Gobaz · May 29, 2016 at 06:45 AM 1
Share

first: put a print in top of OnCollisionEnter2d to see if it gets there "print(other.name)" the if statement looks good.

avatar image andrew23890 Gobaz · May 30, 2016 at 05:32 AM 0
Share

thanks for confirmation on the if statement. it is indeed not getting there. I have public void OnCollisionEnter2d(Collision2D other) { print(other.transform.name); if...

it didn't seem right to put it outside of the method, and it threw errors, so if I am supposed to place the print statement above the method, could you provide syntax? thanks again!

avatar image andrew23890 Gobaz · May 30, 2016 at 05:58 AM 0
Share

I moved that method right below the declaration of variables, which meant just moving the start method below the oncollisionenter2d method. (are they called methods? I've said it in my head so much it just sounds weird now.) but still no go. im going to investigate issues with my prefab.

avatar image UsmanAbbasi · May 29, 2016 at 08:16 AM 1
Share

Comparing with name is not a good idea when you are instantiating the objects. Ins$$anonymous$$d assign tag to the prefab and compare using tag like this:

 if (other.gameObject.tag== "AttackSpellPrefab")
avatar image andrew23890 UsmanAbbasi · May 30, 2016 at 05:38 AM 0
Share

I was trying to make exactly that happen. I guess i was way off? I had [SerializeField] bool IsAttackSpell = true in the script for the attack spell. on the attack spell prefab, this gives me a checkbox for that purpose. I couldn't figure out how to reference that variable in script. How wrong was I in doing it that way?

avatar image andrew23890 UsmanAbbasi · May 31, 2016 at 02:38 PM 0
Share

@UsmanAbbasi I assigned a tag to the prefab for the attack spell, but when it pulls the name in code, it is saying Untagged. I think it's trying to give me the players tag, since the player originates the attack. do I need to do something like gameObject.instantiatedObject.tag? it is only happening when my spell sprite hits the enemy sprite, and when I pause and step through the code, I see my tag on the instantiated clones.

avatar image UsmanAbbasi andrew23890 · Jun 01, 2016 at 08:49 AM 0
Share

Print(Debug.Log) the name of the gameobject whose tag you are accessing to make sure what gameobject it is checking.

Show more comments
avatar image Gobaz · May 29, 2016 at 08:57 AM 0
Share

Its a good priciple but its not specific to his problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew23890 · Jun 01, 2016 at 07:58 PM

To resolve my main issue i used a second hitbox and made it a trigger with no material, then used ontrigger instead of oncollision. I also assigned a tag to the attack and use the tag to identify the gameobject. It now calls the if statement, checks if it's an attack, and reduces the enemies health, prints the enemies health, declares it has been hit.

I'm now working on getting

     IEnumerator FlashIt()
          {
              GetComponent<SpriteRenderer>().enabled = false;
              yield return new WaitForSeconds(.5f);
              GetComponent<SpriteRenderer>().enabled = true;
          }
 

to work by calling FlashIt(); in the if statement in the ontriggerenter2d method.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

88 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

Related Questions

OnColliderEnter stopping after a few seconds! 1 Answer

How to interrupt wait coroutine in a loop? 0 Answers

Let the Camera Face the middle of the Map while still following the Player? 1 Answer

Flip 3D Character Rotation 180 on Y axis 1 Answer

How to fix snake behavior in Unity3d? 0 Answers


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