- Home /
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!
No Luck Now It didn't work. Now Im getting this error code
Assets/abc.js(1,40): BCE0043: Unexpected token: theCollider.
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).
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");
}
}
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
Collider2D needs to be Collision2D, Collider2D is for triggers not Collisions.
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
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?
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
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
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')?
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
Try double clicking that error. It should take you to the problematic code that's throwing it.
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
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");
}
}
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.
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.
Then open de animation window and create an animation clip from there.
Add your curves and animate
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.
If you want to add more animations to the GameObject, do so but don't forget to do step 4 first.
Hope this helps
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
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. :(
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
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
Follow this Question
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