Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
RanBowJet

Change Player "Avatar" to a other "Avatar" by a GUI button click?

Question

Hey ArmedUnity forum, I'm working on a cool game with the awesome Unity engine and now I need help with one thing.


I wan't to change the "Avatar" from my player to a other "Avatar" by a GUI button click.


My question is, how can I do that?


Thanks;)


Screenshot:

Change.png


  [1]: Please login or register to see this link.

Share this post


Link to post
Share on other sites

18 answers to this question

Recommended Posts

  • 0

public Avatar[] avatars;

public Animator anim;

 

private int selected = 0;

 

public void Start()

{

anim.avatar = avatars[0];

}

 

public void Increment()

{

selected++;

if(selected >= avatars.Length) selected = 0;

anim.avatar = avatars[selected];

}

 

public void Select(int index)

{

if(index < 0 || index >= avatars.Length) return;

anim.avatar = avatars[index];

selected = index;

}

 

then just choose one one of the functions to call from your uibutton

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)
Please login or register to see this quote.

Thanks for your helpful answer, it work.

I have one another question, do you know, how can I disable a gameobject with a timer.

So if I click on the UI button, is the selected gameobject disabled after 2 seconds?

Edited by ImHaxoTV

Share this post


Link to post
Share on other sites
  • 0

Simplest solution for you right now would be using Update(), float as timer and add component

This approach basically 1. adds component to a gameobject 2. runs update and reduces timer 3. kill itself, disables gameobject when timer finishes

 

Its usage would be

Please login or register to see this code.

 

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0

Or you could more simply use a WaitForSeconds in a coroutine :

Please login or register to see this link.

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Hey, thx for your answer. Do you know, how can I spawn a prefab on my player position by click on a UI button?

Share this post


Link to post
Share on other sites
  • 0

Note that 

Please login or register to see this code.

will work corectly on if script executing it exists on player gameobject, if it exists in ui it will spawn in probbaly far away where ui recttransform position is

if it on ui simply add

 

public Transform player; //drag and drop player transform

 

and then call it Spawn(player.position);

 

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Thanks, question: How can my UI button activate the "Spawn(transform.position);" call?

Share this post


Link to post
Share on other sites
  • 0

Unity has very good tutorials on this matter, they also many others check them out
Please login or register to see this link.

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Ok, but can you tell me, what kind of function must I choose?

 

Unbenannt.jpg

Share this post


Link to post
Share on other sites
  • 0

With buttons you basically have two options to flow your logic 1. is using method unity tutorial gave you which is basically you have to manually drag and drop and select function 2. you subscribe to your buttons manually using event listeners.
Right now you should stick to 1. one
 

You should probably create new method, which basically is a wrapper for method i wrote before

 

public void Spawn(){

    Spawn(transform.position);

}

and as your function select Spawn()
 

  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)
Please login or register to see this quote.

Hey, I have one last question. 

 

I have created a own script, that should disable a another script if I click on a UI button. But it don't work, what did I do wrong?

 

Code:

 

Please login or register to see this code.

 

Unbenannt.jpg

Edited by ImHaxoTV

Share this post


Link to post
Share on other sites
  • 0

You sure your ui button event is even fired? Add Debug.Log("works"); inside DisableScript to see if your ui button even works

edit: if nothing appears in Console the problem is most likely that your scene does not contain EventSystem or your button doesnt even have raycastable element (such as image or RawImage) or you have some 3rd invisible ui element on top of it blocking the click ray

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Ok and how can a gameobject disable a script after the gameobject is enabled?

So the "disable script" is on the disabled gameobject and if I enable the gameobject via my UI button, will the "disable script" automatically activate.

Share this post


Link to post
Share on other sites
  • 0

Script disable is done just like you did script.enabled = false; to disable gameObject you use gameObject.SetActive(false); if you enable/disable gameObject, components will not be automatically enabled

note that script enable/disable affects only for unity ghost methods, such as OnEnable(), Update(), OnTriggerEnter() etc

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)
Please login or register to see this quote.

Hey, I have a question to a function, it wourld be cool if you can help me.

 

I wourld insert a function to change the animation controller for 20 secounds from the selected player, if the player shoot/hit at enemy. 

And then will it switch back to the old animation controller, so after 20 secounds.

 

So, the player has a normal idle, walk, meele attack in the first animation controller. And if the player hit the enemy will the first normal animation controller switch to the other controller with the attack idle, attack walk etc and after 20 secounds is the normal animation controller back. So the player have again the normal animations.

 

Do you know how can I make it?

 

The code:

Please login or register to see this code.

 

Edited by ImHaxoTV

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

I have created a own code, so it woks now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×