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 /
  • Help Room /
avatar image
0
Question by Aepmobile · May 07, 2016 at 05:15 PM · scripting problemcontrolleranimator controllerspriterenderer

OnClick change Animator & Sprite Renderer

In the menu of my game, have an option to change the skin.

How do I, so that when the user clicks on "change," and whe he enter the game the "Controller Animator" and "Sprite Renderer" is changed?

Animators: Bee, Bee1, Bee2, Bee3, and Bee4

Spriter rederers: Bee_0, Bee1_0, Bee2_0, Bee3_0 and Bee4_0

It's possible "OnClick" change Controller "Bee" to "Bee1" & Sprite Renderer "Bee_0" to "Bee1_0" ?

alt text

My "Bee" (PLAYER) script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class Bee : MonoBehaviour {
 
     public float moveSpeed;
 
     public Transform bee;
     private Animator animator;
 
     public bool isGrounded = true;
     public float force;
 
     public float jumpTime = 0.1f;
     public float jumpDelay = 0.1f;
     public bool jumped = false;
     public Transform ground;
 
 
     // Use this for initialization
     void Start ()
     {
         animator = bee.GetComponent<Animator> ();
     }
     
     void Update ()
     {
         Move ();
   }
 
 
     void Move ()
     {
 
         isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor"));
         animator.SetFloat ("runB", Mathf.Abs (CrossPlatformInputManager.GetAxis ("Horizontal")));
         if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") > 0) {
 
             transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2 (0, 0);
         }
         if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") < 0) {
 
             transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2 (0, 180);
         }
 
         if (CrossPlatformInputManager.GetButtonDown ("Vertical") && isGrounded && !jumped) {
 
             //  rigidbody2D.AddForce (transform.up * force);
             GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
             jumpTime = jumpDelay;
             animator.SetTrigger ("jumpB");
             jumped = true;
         }
 
         jumpTime -= Time.deltaTime;
 
         if (jumpTime <= 0 && isGrounded && jumped) {
 
             animator.SetTrigger ("groundB");
             jumped = false;
 
         }
 
     }
 
 }    


4.png (202.7 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 TheSorm · May 07, 2016 at 09:17 PM

You have to Options I. Make a few Prefabs the Player can chose from

II. Write a Method like this and attach it to your Button OnClick() Event:

     puplic RuntimeAnimatorController anim;
     
         puplic void ChangeAnimator() {
               Animator animator = playerTransform.gameObject.GetComponent<Animator>();
               animator.runtimeAnimatorController = anim;
     }






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 Aepmobile · May 08, 2016 at 09:48 PM 0
Share

I try,

using UnityEngine; using System.Collections;

public class Bee4 : $$anonymous$$onoBehaviour {

 public RuntimeAnimatorController anim;
 public Transform bee4;

 public void ChangeAnimator() {
     Animator animator = bee4.gameObject.GetComponent<Animator>();
     animator.runtimeAnimatorController = anim;
 }

}

But not change in game.

avatar image TheSorm · May 08, 2016 at 09:53 PM 0
Share

are you shure you edit an existing player who is used for the game ? or dose the game player is instaziate after that with a prefab ?

avatar image Aepmobile · May 08, 2016 at 10:56 PM 0
Share

@TheSorm look my new asnwer with some pics, if possible =] ty for help

avatar image
0

Answer by Aepmobile · May 08, 2016 at 10:54 PM

Look how going my project.

At MenuShopCanvas, I have attached "Choice" script on the button

Imagens: http://imgur.com/a/aJTBE

 using UnityEngine; using System.Collections;
 
 public class Choice : MonoBehaviour {
 
     public RuntimeAnimatorController anim;
     public Transform bee;
 
     public void ChangeAnimator() {
         Animator animator = bee.gameObject.GetComponent<Animator>();
         animator.runtimeAnimatorController = anim;
     }
 }

But when i click "Choice" or "Default" nothing happens.

I put the "Controller Animator" which was created for each prefab, correct?

What am I doing wrong? When I click "Play" no prefab is instantiated.

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 TheSorm · May 09, 2016 at 02:30 PM 0
Share

Do you add the ChangeAnimator() function to the onClick function of the button in the editor?

avatar image Aepmobile · May 09, 2016 at 10:18 PM 0
Share

Yes, i drag and drop "Choice" button at each OnClick (), and select ChangeAnimator(), but when i click play no have player in scene.

avatar image TheSorm · May 10, 2016 at 02:22 PM 0
Share

do you have a game object where the Choise skript is attached ? or do you attached it to all buttons ? it has to be all at one instance of the script and you need a method that the game can take the edited bee and use it.

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

61 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

Related Questions

Jump problem animation 0 Answers

UnityEditor.Animations API in a build 2 Answers

assigning sprites to game object via script 0 Answers

have an error that i don't know how to fix 1 Answer

Why do i keep getting the error 'Animator is not playing an animatorcontroller' when is appears to be? 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