• 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
2
Question by PrimeDerektive · Feb 19, 2010 at 04:28 PM · animationgameobject

Trouble accessing animations from other GameObject

So I'm trying to access my object tagged "Player" animations in the script that is on his gun, so I did this(I'm slimming it down just to ask about the important parts):

Code:

function Update(){
   var player = GameObject.FindWithTag("Player");
   player.animation.Play("Shoot");
}

This works fine, but I want to change the animation's WrapMode to once, so I need to find the animation in Start(), before Update().

But, if I create the "var player" in Start(), Update() can't use that variable. Should I create it twice?

Or can I create it outside of all the functions at the top as a private variable?

Comment
Add comment · Show 1
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 cregox · Feb 19, 2010 at 05:57 PM 0
Share

From this title I initially thought you were trying to access variables from one SCRIPT to another, rather than functions. :P

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by duck · Feb 19, 2010 at 04:36 PM

Yes, if you create your variable outside all the functions, and the variable will persist throughout all the functions:

var player : GameObject;

function Start() { player = GameObject.FindWithTag("Player"); }

function Update(){ player.animation.Play("Shoot"); }

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 PrimeDerektive · Feb 19, 2010 at 04:45 PM 0
Share

Ohhhh I see! You can declare the variables but thats it.

I was trying the actual:

var player = GameObject.FindWithTag("Player");

outside of the functions.

I would probably want to make that var private though, right? There'd be no sense in exposing it to the editor if your going to set it in Start() anyway, right?

avatar image duck ♦♦ · Feb 19, 2010 at 04:58 PM 0
Share

Yes, in this case it makes good sense to make it a private var.

avatar image duck ♦♦ · Feb 19, 2010 at 04:58 PM 0
Share

P.S. please remember to tick the 'accept' button if your question is solved!

avatar image
0

Answer by urawhat · May 04, 2012 at 07:35 PM

var player : GameObject;

function Start() { // tag you object to player player = GameObject.FindWithTag("Player"); }

player.animation.wrapMode = WrapMode.Loop;

player.animation["jump"].wrapMode = WrapMode.Clamp; player.animation["shoot"].wrapMode = WrapMode.Clamp;

player.animation["idle"].layer = -1; player.animation["run"].layer = -1;

player.animation.Stop();

function Update () { if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1) player.animation.CrossFade("run"); else player.animation.CrossFade("idle"); if (Input.GetButtonDown ("Jump")) player.animation.CrossFade("jump");

 if (Input.GetButtonDown ("Fire1"))
        player.animation.CrossFadeQueued("shoot", 0.3, QueueMode.PlayNow);

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How could it be done "Cooldown" for a shield ? 2 Answers

How do I access the current playing animation clip of a gameObject? 0 Answers

setTrigger() for another GameObject doesn't work 1 Answer

Rotating Door 2 Answers

how to open two classic double door and windows 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