Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ger122 · Nov 30, 2014 at 07:35 AM · collisionontriggerentertriggersbce0043

Some1 PLEASE HELP I SPENT HOURS trying 2 fix but nothing. My trigger / animation wont play on collision

Hi guys please help I tried everything I know spent hours no luck.

I have Gameobject/image and want to play a animation on collision on it but nothing is working

I tried tons of combos and options. I have a small question..If you drag an image onto scene is that considered a gameobject?- maybe that the problem??

But I added rigigbodies 2d 2d colliders 2d oncollision on trigger ect etc I checked a lot of basics And tried a lot of scripts from references area

Nothing happens on any collision I set trigger to gameobject.

I see animation playing rapidly but will not play on collision at all. But animation is working when reviewed in animation inspector area

Anyway I currently have this (javascript)

 function OnCollisionEnter2D(Collider2D) {
 
     if(gameObject.tag == "Player")
  {
          animation.Play("crack");
      }
  }

I also tried OnTriggerEnter 2d etc scripts & more I added this to game object but nothing happens

the animation keeps playing constantly with things I did in animator controller. I tried looking at animator controller animations scripts still nothing. My animation just keeps playing from the botched( I think botched) parameter I put thru animator controller

I did add this to correct gameobject and tagged other gameobject correctly as player
I tried keeping this simple but still not working. So here is 1 scripts I tried above. Please post correct script for me so I can add it to my image/gameobject and move on.

PLEASE PLEASE Someone who is good at this HELP!I literally spent a ton of time and need pro help!

Comment
Add comment · Show 2
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 ger122 · Nov 30, 2014 at 12:47 PM 0
Share

No Luck Now It didn't work. Now Im getting this error code

Assets/abc.js(1,40): BCE0043: Unexpected token: theCollider.

avatar image tanoshimi · Dec 06, 2014 at 11:11 PM 1
Share

If this question is still unanswered, please update the original question above with the latest code you are using (I think you've already had pointed out to you several problems with what is currently written).

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JSierraAKAMC · Nov 30, 2014 at 07:56 AM

I think your problem is that you're not checking the collided object's tag--you're checking the tag of the GameObject this script is on. Try this:

 function OnCollisionEnter2D(Collider2D theCollider) {
  
      if(theCollider.gameObject.tag == "Player")
      {
           animation.Play("crack");
      }
   }
  
Comment
Add comment · Show 13 · 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
avatar image ger122 · Nov 30, 2014 at 12:49 PM 0
Share

No Luck Didn't work Now Im getting this error code ,below

Assets/abc.js(1,40): BCE0043: Unexpected token: theCollider.

I made sure All objects have rigid2d bodys and 2d colliders But I am getting this error

avatar image MrSoad · Nov 30, 2014 at 01:15 PM 0
Share

Collider2D needs to be Collision2D, Collider2D is for triggers not Collisions.

avatar image ger122 · Nov 30, 2014 at 01:24 PM 0
Share

I think our right

But now when player hits gameobject I get a pause in game no animation plays and say this error--

NullReferenceException: Object reference not set to an instance of an object

avatar image MrSoad · Nov 30, 2014 at 01:27 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object means you are trying to do something with an object that is missing(in terms of the script not having an up to date reference to this object). This is a new question and should be posted as such, the code you have given here is of no use for trying to track down this new error. Accept JSierra's answer and post a new question with the whole script and your full error message(inc line number), thanks.

EDIT : I get the feeling this is your full code from another read, yes? If so what is the full error message inc line number, is it on line 5? If so then the script prob cannot find the "crack" animation. Is this on the same gameobject as this script?

avatar image ger122 · Nov 30, 2014 at 02:07 PM 0
Share

Yes but I tried few things and that error went away but I now have the problem of the animation still will not play.

I am trying every option here but still nothing.

I have used these codes and attached it to the gameobject

 function OnCollisionEnter2D(coll: Collision2D) {
     if (coll.gameObject.tag == "Player")
         animation.Play("crack");
 }


and also tried this code

 function OnCollisionEnter2D(coll: Collision2D) {
 
     if(gameObject.tag == "Player")
  {
          animation.Play("crack");
      }
      }


But nothing is working

I am really searching and trying here but cannot resolve this

The animation will not play on collision, The objects just bounce eachother around as colliders should but No animation

Thank yOU

Show more comments
avatar image
0

Answer by ger122 · Nov 30, 2014 at 03:31 PM

Yes I tried few things and that error went away but I now have the problem of the animation still will not play.

I am trying every option here but still nothing.

I have used these codes and attached it to the gameobject

 function OnCollisionEnter2D(coll: Collision2D) {
     if (coll.gameObject.tag == "Player")
         animation.Play("crack");
 }


and also tried this code

 function OnCollisionEnter2D(coll: Collision2D) {
 
     if(gameObject.tag == "Player")
  {
          animation.Play("crack");
      }
      }


But nothing is working

I am really searching and trying here but cannot resolve this

The animation will not play on collision, The objects just bounce eachother around as colliders should but No animation

Thank yOU

Comment
Add comment · Show 7 · 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
avatar image JSierraAKAMC · Nov 30, 2014 at 06:14 PM 0
Share

Silly me, I was using a bit of C# in my answer when this is clearly JavaScript. Now, let's get to the bottom of this. Is your animation called "Crack" (with a capital'c') or "crack" (with a lowercase 'c')?

avatar image ger122 · Nov 30, 2014 at 06:29 PM 0
Share

crack lower case

I always am careful for details like that spelling cases always same

I am trying but nothing is working

currently I am getting the error code

NullReferenceException: (null) UnityEditor.SerializedObject..ctor (UnityEngine.Object obj) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/SerializedPropertyBindings.cs:66) UnityEditor.Graphs.AnimationState$$anonymous$$achine.TransitionInspector.SetTransitionToInspect (UnityEditorInternal.Transition transition) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Graphs/UnityEditor.Graphs/AnimationState$$anonymous$$achine/TransitionInspector.cs:176) UnityEditor.Graphs.AnimationState$$anonymous$$achine.TransitionInspector.OnEnable () (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Graphs/UnityEditor.Graphs/AnimationState$$anonymous$$achine/TransitionInspector.cs:99)

At first the new animation clips I make play but then they just do not attach or function with the gameobject

avatar image JSierraAKAMC · Nov 30, 2014 at 06:36 PM 0
Share

Try double clicking that error. It should take you to the problematic code that's throwing it.

avatar image ger122 · Nov 30, 2014 at 06:41 PM 0
Share

ok I did it was shaded grey and seemed to have brought me to inspector tab which was already open but nothing is there

Its all grey

avatar image ger122 · Nov 30, 2014 at 06:43 PM 0
Share

again, every time object collides with player I do get the message collision detected as normal but no animation plays??

I have animations linked to script and to the animation control in inspector sidebar.

THIS is current code I have attached to object.

 function OnCollisionEnter2D(coll : Collision2D) {
    
      if (coll.gameObject.tag == "Player") {
  
          Debug.Log("Player collision detected");
          animation.Play("crack");
      }
  }


Show more comments
avatar image
0

Answer by glantucan · Dec 04, 2014 at 07:29 AM

I don't think your problem is in the code. I have found similar problems when creating and using old style animations (not mecanim). It seems to be a bug introduced in the last versions. Unfortunately when this happened to me I had to recreate the animations. For the time being what I do and recomend is to follow these exact steps when creating animations.

  1. DO THIS FIRST. Add the animation component to the GameObject to be animated. If you don't, an Animator component is created and that interferes with the old style animations.

  2. Then open de animation window and create an animation clip from there.

  3. Add your curves and animate

  4. As soon as you finished your first animation, go to your Animatiosn project folder and drag the animation clip to the Aimation property of the Animation component in the inspector, so this one is the default animation. Most of the times I found problems I had forgotten to do this.

  5. If you want to add more animations to the GameObject, do so but don't forget to do step 4 first.

Hope this helps

Comment
Add comment · Show 4 · 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
avatar image ger122 · Dec 04, 2014 at 01:57 PM 0
Share

I tried this I started a new project just now

Heres what I did exactly!

1 started a new project

2 dragged images form my files into unity

3 gave all proper 2d attributes colliders,rb2ds etc

4 added animation component to object

5 made animation clip from images I dragged in

6 attached clip from project folder like you said to animation area(animation clip)

7 $$anonymous$$ade 2 scripts (1 of them,play animation on 2dcollison (java) and added to objects.

8 tagged player as player.

9 animation wont play so I added animator component to the inspector Now animation will play but not stop But WILL NOT PLAY ON COLLISION

10 Tried $$anonymous$$aking clip to legacy ,(1)(when on legacy animation doesn't play at ALL either way) So went back to making non legacy (switched to 2) Now will play but NOT on collision just plays and will not stop

Yes I realized this also it is something like a bug in the setup of the animation component area or animator area I read in my research that this must be installed in a order fashion to work but Even though I tried still nothing

I appreciate any help and hope to hear back from you

avatar image glantucan · Dec 04, 2014 at 09:14 PM 0
Share

The steps I mentioned before only work (for me at least) without using the Animator component, which belongs to the mecanim system and needs a properly configured state machine. I don't use or know the mecanim system very well so I can't help you there if there's where you want to go.

When followed thoroughly this steps should configure the animationClips as legacy animations automatically, so you shouldn't have to change that setting using the inspector in debug mode.

If this doesn't help you out I'm afraid I'm as lost as you are. :(

avatar image ger122 · Dec 04, 2014 at 11:49 PM 0
Share

Its a bit confusing to say the least.. I have done research and many say use both animation and animator to work and few do say also don't use animator component but when I just removed it again the animations wont play at all when reinstalled they do play also here is a 2d ONLY tutorials form unity headquarters stating to use both also

http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

This vid shows helpful tings but doesn't go in2 detail on details like with this issue.

But again I am using unity 4.5 free version and I see so far animator is needed for even 2d animations

I tried all you said again but nothing.

I will keep trying

I also just though of something do I need a link to the animation/animator maybe to the script code? I have only collision enter etc but no reference to anim/ animator get component all I have is this collision script in java below

 function OnCollisionEnter2D(coll : Collision2D) {
        
          if (coll.gameObject.tag == "Player") {
      
              Debug.Log("Player collision detected");
              animation.Play("hit");
              
          }
      
      }



message e if any ideas im OPEN!!

thanks

avatar image glantucan · Dec 06, 2014 at 10:29 PM 0
Share

Ok, let's clarify a couple of things and see if that helps.

There are two of animation systems built in together right now in Unity.

The old one, also called in the docs "Legacy animation system".

The new one, called "$$anonymous$$ecanim".

$$anonymous$$ecanim is an extension of the old one, adding up to it the animator component which enables the use of a state machine to control and blend the transitions between the different animation clips we might have attached to the animation component.

I don't use $$anonymous$$ecanim cause my animations use to be simple and not more than two or three for the same game object. A State machine to control transitions looks like an overkill to me in those cases.

Besides, $$anonymous$$ecanim needs the animationClips to be stored in a different format which is not compatible with the use of Animation component alone. So as soon as you add an animator component all animation clips created for that gameobject get formatted for $$anonymous$$ecanim.

"Theoretically" you can format them back to the legacy system by selecting them in the project window, switching the inspector to debug mode and setting the Animation Type to 1. But somehow they introduced a difficult to track bug here and most of the times that doesn't work.

I had to repeat several times all posible combinations to figure out the procedure to get the legacy animations to work. It's not documented anywhere AFAI$$anonymous$$, but the steps I told you work well for legacy animations (at least in 3D). But the rule I ALWAYS follow is not to add the animator component EVER, as I don't want to use mecanim, and that usually mess up the animation clips if you want to use them in the legacy system.

If you want to use mecanim I think you need to configure the state machine in the animator component properly, but don't trust me on this because I never used it.

Regarding your code: Is this script attached to the gameobject tagged as "Player" or to the other one? Your coll parameter referes to the collider colliding with the gameobject you have the script attached to. So you are trying to play the animation "hit" in a gameobject only if the other collider is attached to the player. Is that what you want?

BTW, no pun intended :), the language you are using is not Java, it is UnityScript. Some people (even the Unity docs) call it javascript (which has nothing to do with Java). But that is not true either, as javascript is actually a very different language even though its syntax may look very similar at first.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Game pauses sometimes with OnTriggerEnter 1 Answer

How do I ignore trigger objects for collision? 0 Answers

OnTriggerEnter is not working! I have tried absolutely everything I can think of. 1 Answer

OnTriggerEnter Multiple Collisions Activating. I tried many variations, Please help (Java) 3 Answers

How to detect collision without OnCollisionEnter or OnTriggerEnter? 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