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 /
This question was closed Apr 30, 2019 at 12:39 AM by lorenzo1812 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by lorenzo1812 · Apr 28, 2019 at 11:11 PM · animation problem

Animation Trigger

Hi! So here i have a script wich enables my screen fading animation whenever i collide with the object called: Ground [UNREACHABLE]. the animation gets disabled when i start the game, but it won't turn back on when i collide it with my player (Character).

Can someone please help me fix this! I am pretty bad at coding so i'd REALLY appriciate it if you tell me on how i can fix it. Thank you!

Fade script (c#):

public class Screen_Fade : MonoBehaviour {

 public Animator anim;

 private void Start()
 {
     anim = GetComponent<Animator>();
     anim.enabled = false;
 }
 void OnCollisionEnter2D(Collision2D col)
 {

     if (col.gameObject.name.Equals("Ground [UNREACHABLE]"))
     {
         anim = GetComponent<Animator>();
         anim.enabled = true;
     }


 }

}

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Nachiten · Apr 29, 2019 at 02:15 AM

@lorenzo1812 The answer is pretty simple and should have a very easy fix. Unity cannot access nor modify hidden or disabled objects. Unless, you already have a variable assigned to them. Which is exactly what you have here.

So I dont really know the reason of why you are changing the value of the variable "anim" to exactly the same value as above. Then unity is not able to find that component as its already disabled. You just remove that and you are done. Good luck !!! I hope that works then tell me if not.

 public Animator anim;
  private void Start()
  {
      anim = GetComponent<Animator>();
      anim.enabled = false;
  }
  void OnCollisionEnter2D(Collision2D col)
  {
      if (col.gameObject.name.Equals("Ground [UNREACHABLE]"))
      {
          // Removed just this line : anim = GetComponent<Animator>();
          anim.enabled = true;
          // This should work just fine
      }
  }
 }



Comment
Add comment · Show 3 · 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 lorenzo1812 · Apr 29, 2019 at 10:13 PM 0
Share

Hi again! Not sure but i changed it like you showed me, but it still doesn't work! Do you have any other solutions? I'v heard that if you disable an animation you can't turn it back on again by script, it that true?

I have been struggling to get this simple thing working for 2 days ;-; All i want is whenever my 2D character gets in contact with the collision of the [UNREACHABLE] ground, the animation starts working.

note that i am using a image on a canvas wich uses an animation that goes from zero opacity to around 65% opacity, so all i did was i used the animator, made 2 keys that go from 0 to 65%, and i only have one animation state, wich is the one from the opacity that immediatly starts when i run the game. I have the script attached to the image.

avatar image Nachiten lorenzo1812 · Apr 29, 2019 at 10:36 PM 0
Share

@lorenzo1812 Could you please tell me if you get any kind of error on console when playing the animation? I mean when excecuting this piece of code:

 if (col.gameObject.name.Equals("Ground [UNREACHABLE]"))
       {
           // Removed just this line : anim = GetComponent<Animator>();
           anim.enabled = true;
           // This should work just fine
       }

I think I may have some other solutions. Like using commands like animation.Play("NA$$anonymous$$E") and animation.Stop("NA$$anonymous$$E"). For this solution you would first need to assign your variable in this case animation and the animation component of the object adding it in the inspector beforehand and not the animator.

And make a code like so:

 // Yes this is an animation
 Animation animation = GetComponent<Animation>();
 
 // To play animation [ Replace Name with your animation name]
 animation.Play("NA$$anonymous$$E");
 
 // To stop animation
 animation.Stop("NA$$anonymous$$E");

This is the reference for all of the above: https://docs.unity3d.com/ScriptReference/Animation.html

Last step if none of this work would be to implement a complete Animator system using the stuff that @thenebularofficial already said above.

This is the reference for that: https://docs.unity3d.com/ScriptReference/Animator.html

Im not qualified to $$anonymous$$ch yoy about this I dont know it a lot myself. Look for tutorials there are lots of them complete and well explained on the web. Your problem should not be hard to adress. Just dont give up. Best of luck.

avatar image lorenzo1812 Nachiten · Apr 30, 2019 at 12:30 AM 0
Share

it worked! I did a quick search in the Unity docs you sent me, and i think i got it right! What i did was i set my animation sooo slow that it's impossible to notice (since it's a white image that slowly fades in), you could only probably see a difference in the game if you left it running for like 3 days straight, wich im pretty sure no mad man would do. The rest was pretty straight forward. I made the speed normal when i collide with the [UNREACHABLE] object, so it's looks totally normal to the people playing it, as they think it's just s simple fade transition!

Thank you for helping me, man! <3

avatar image
1

Answer by thenebularofficial · Apr 29, 2019 at 02:08 AM

Okay, there might be multiple reasons for your problem, so here are some suggested fixes: 1.Make sure the animator's default state is the animation you want. For more information, visit this Unity Tutorial.

2.If you don't want to use your animation as default state try using Triggers, create one in the editor, make your animation to play when you activate your trigger and add this piece of code anim.SetTrigger("Your_Trigger_name"); Documentation

Also, you don't need to set anim = GetComponent<Animator>(); twice as you already set it in void Start.

Comment
Add comment · Show 1 · 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 lorenzo1812 · Apr 30, 2019 at 12:33 AM 0
Share

Thanks for your help! I REALLY appriciated it! <3

Follow this Question

Answers Answers and Comments

105 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

Related Questions

Animation problem 2 Answers

Animation issues, how to fix or report problem? 0 Answers

Character has free movement when 'shooting' 0 Answers

Animation rigging play mode and build difference 0 Answers

animation.Isplaying and waitforsecond(animation.length) not end at the same time 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