- Home /
Code seems to be giving priority to the first if written rather than giving them equal value
So I made a follower switcher, and I had this problem before, but I forgot what I did to fix it. Here's my code:
using UnityEngine;
using System.Collections;
public class OneFollower : MonoBehaviour {
private Follower HumanFollowerGet;
private Follower AlienFollowerGet;
public Follower CurrentFollower;
private bool HumanFollowerStateGet;
private bool AlienFollowerStateGet;
private bool CurrentFollowerStateGet;
void Start () {
HumanFollowerGet = GameObject.Find ("Human Follower").GetComponent <Follower> ();
AlienFollowerGet = GameObject.Find ("Alien Follower").GetComponent <Follower> ();
}
void Update () {
HumanFollowerStateGet = HumanFollowerGet.GetComponent <Follower> ().Following;
AlienFollowerStateGet = AlienFollowerGet.GetComponent <Follower> ().Following;
Debug.Log (HumanFollowerStateGet);
Debug.Log (AlienFollowerStateGet);
if (AlienFollowerStateGet == true)
{
ChangeFollower (AlienFollowerGet);
}
if (HumanFollowerStateGet == true)
{
ChangeFollower (HumanFollowerGet);
}
}
void ChangeFollower (Follower NewFollower) {
if (CurrentFollower != null)
{
CurrentFollower.Following = false;
}
CurrentFollower = NewFollower;
CurrentFollower.Following = true;
}
}
if I put HumanFollowerStateGet before AlienFollowerStateGet, for example, and then I approach the human follower, it works, then I approach the alien follower and then back to the human and the human Following state is in both true and false if im within the meet distance. It is supposed to do this, but the code is arguing the command back and forth. I once fixed this without any additional code. Help?
What is the situation you are using this code in and what is this script attached to? And what is the desired behavior?
Where do you set Follower.Following
to true initially?
If you set Follower.Following
to true when you get close enough to either the alien or human, does this first follower then follow you so that when you reach the next one, you are simultaneously inside both meet distances?
In that case both HumanFollowerStateGet
and AlienFollowerStateGet
would be true in the next Update() which would probably cause unwanted behavior.
Follower.Following is set to true when the follower is in range of meeting up with the player, and is set to false when the player is too far away, two separate values. The OneFollower script is attached to both the followers.
This worked once but i restarted the game because i lost half the data. This is exactly how I did it last time though. $$anonymous$$y intentions is that when a second follower is in range, get rid of the first.