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 /
avatar image
0
Question by NKolda · Jan 30, 2019 at 11:52 AM · 2danimatortrigger

Object script triggering player animation

Ok So I've got this block of code as of right now for when my player hits the ball. Right now, this does what I want it to do: -checks the position of the ball. -gets rid of the ball if the user presses a button at the right time.

Now, this script is attached to the ball and not the player, so I wouldn't be able to set a trigger from the player's animator like I normally would. Is there a line of code I can put so that I can utilise the player animator here?

alt text

unity.png (13.1 kB)
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

· Add your reply
  • Sort: 
avatar image
0

Answer by sirjoan620 · Jan 30, 2019 at 01:15 PM

You can use this code but it's not optimal.

 FindObjectOfType<PlayerScript>().GetComponent<Animator>().SetTrigger("Start");

Check this out: Animator.SetTrigger

Or better way,

 [SerializeField] private Animator playerAnimator;
     
  private void Hit()
  {
      if(yourStatement)
      {
          playerAnimator.SetTrigger("Start");
          Destroy(gameObject);
      }
  }

Comment
Add comment · Show 2 · 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 NKolda · Feb 06, 2019 at 12:59 AM 0
Share

Ok so I was trying the top one`myAnimator = GameObject.FindObjectOfType().GetComponent();`

Didn't work :/. I was assu$$anonymous$$g where you put "PlayerScript", I'd just put the name of the player object - am I wrong there? Then it comes up with this error which I can't quite figure out: " The type or namespace name 'PlayerP' could not be found (are you missing a using directive or an assembly reference?)"

avatar image sirjoan620 NKolda · Feb 06, 2019 at 09:10 AM 0
Share

FindObjectOfType finds the component by searching all the scene objects. So you should give the component not the name of gameObject.

Look: FindObjectOfType Docs

avatar image
0

Answer by Chimer0s · Feb 06, 2019 at 12:21 PM

You could create a static instance of a script attached to your player object and reference that. For example, say you have a script attached to your player object called "PlayerController." In that script you would have something like

 {    
     public static PlayerController instance;
     
     void Start(){
     if (!instance)
         instance = this;
     }
 }

Then in your ball code you could have something like

   private void Hit()
   {
       if(transform.position.x  > -5 && transform.position.x < 1 && Input.GetKeyDown(KeyCode.LeftShift))
       {
           PlayerController.instance.GetComponent<Animator>().SetTrigger("Whatever");
           Destroy(gameObject);
       }
   }

I'd point out that it's important to have the animation event triggered before destroying the gameobject, unlike you have in your image as once the gameObject is destroyed, the script will go with it and fail to complete anything you had set to happen after.

Also, you can only have a single static instance of a script at a time. So if you were to have multiple player gameobjects with the PlayerController script, this wouldn't work. The beauty of static instances such as these is that you don't need an object to reference them, as you can see in the block of code. It's as easy as directly referencing the script.instance. This is far faster and more efficient than trying to search for a game object by tag or component.

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

225 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 Animation does not start 1 Answer

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

All of my doors are opening at the same time. Why? 0 Answers

Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 1 Answer

Animation not playing! Help needed! 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