Really don’t get this:
I have 2 UI objects, eachone having its one toggle, from the toggles I call 2 functions from the same script. The first function is the same for both GameObjects, which is an animation.Then I have 2 more function in order to choose which object should animate:
private Animator anim;
public GameObject target;
public GameObject panelCamera;
public GameObject panelEnvironment;
void Start () {
anim = target.GetComponent<Animator>();
}
.....
public void chooseCamera(){
target = panelCamera;
}
public void chooseEnvironment(){
target = panelEnvironment;
}
Now why if I put anim = panelCamera.GetComponent(); or anim = panelEnvironment.GetComponent(); it works for each chosen one and if I use
anim = target.GetComponent(); it always uses panelCamera although in the editor I can see the switch?
Hi,
That’s because you define anim in the Start function, Start function will not be called again so it will not reassign when target change. Unless you directly write it down in Update of any repeatable function, variables are not dynamically changed. When you set up a value it keeps that value while you don’t change it “manually”. I guess you drag and dropped panelCamera in the editor.
What you need to do: in chooseCamera() and chooseEnvironment() you need to add another taget.GetComponent<>() line:
public void chooseCamera () {
target = panelCamera;
anim = target.GetComponent<Animator>();
}
public void chooseEnvironment () {
target = panelEnvironment;
anim = target.GetComponent<Animator>();
}
Hi Thanks a lot, this works!
However I wanted to experiment different things and I end up doing-it like this:
public GameObject panelCamera;
public GameObject panelEnvironment;
public Animator[] alls;
…
public void chooseCamera(){
foreach(Animator all in alls){
all.GetComponent<Animator>().enabled = true;
all.GetComponent<Animator>().Play("PanelSlideOut");
}
panelCamera.GetComponent<Animator>().Play("PanelSlideIn");
}
public void chooseEnvironment(){
foreach(Animator all in alls){
all.GetComponent<Animator>().enabled = true;
all.GetComponent<Animator>().Play("PanelSlideOut");
}
panelEnvironment.GetComponent<Animator>().Play("PanelSlideIn");
}
It works fine but when I click for the first time I get a strange behavior, there must be something wrong with my animator, you can check a test here
p.s. press F1 for controls
Well first, before to play your SlideIn animation you should set the coloured panel you want to rise as last or first sibling. Because right now you don’t have the same behaviour between every cases: green appears atop of red while red appears from behind green so you begin to see it at the middle of your animation (while you see green for all its animation). Switch between red and green multiple times and observe how they are different. If that’s an effect you wanna make, you can keep it ofc ^^
About the initialization: that’s not a problem, that’s just that you ask the “SlideOut” animation for all your panels before to “SlideIn” one panel (and green is atop all the others that’s why you only see this one but they are all moving pilled up, and you only saw the brownish one when you changed the order, because the lower in the hierarchy, the upper in the displayed layer). But since your animation (the animation file) starts with all panels up on first key and then puts them down, when you play the animation it sets the panel in the good state (which is up) and then plays.
So that was the cause, now, solution:
In your button functions you need to know if it’s the first time you slide the panels or not. Let’s use a boolean for that. This boolean must be declared for the whole script so you can click on any button and it will works anyway.
bool firstSlide = true;
Now, in every button function you need to do this change: IF this is the first time i need to slide a panel: don’t play the slide out animation ELSE you can play slide out.
public void chooseXXX () {
if (firstSlide) {
firstSlide = false;
panelXXX.GetComponent<Animator>().Play("PanelSlideIn");
} else {
// stuff you already wrote and that works :)
foreach () {
// ...
}
panelXXX. //...
}
}
I hope it was clear enough for you ^^