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" ?
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;
}
}
}
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;
}
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.
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 ?
@TheSorm look my new asnwer with some pics, if possible =] ty for help
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.
Do you add the ChangeAnimator() function to the onClick function of the button in the editor?
Yes, i drag and drop "Choice" button at each OnClick (), and select ChangeAnimator(), but when i click play no have player in scene.
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.