- Home /
Make it so if another follower starts following you, the one currently following stops?
This is what i have:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject HumanFollower;
public GameObject AlienFollower;
public GameObject RobotFollower;
public GameObject MonsterFollower;
private bool HumanFollowerStateGet;
private bool AlienFollowerStateGet;
private bool RobotFollowerStateGet;
private bool MonsterFollowerStateGet;
// Use this for initialization
void Start () {
HumanFollowerStateGet = HumanFollower.GetComponent <Follower> ().Following;
AlienFollowerStateGet = AlienFollower.GetComponent <Follower> ().Following;
}
// Update is called once per frame
void Update () {
if (HumanFollowerStateGet == true)
{
if (AlienFollowerStateGet == true)
{
AlienFollowerStateGet = false;
}
}
}
}
will this work if i do it for all of them? If not, how will I make it work?
By the way the bools are programmed to get the state of other bools that represent whether that follower is following or not
PLEASE do us all a favour and learn the difference between a value type and a reference type.
http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx
http://msdn.microsoft.com/en-us/library/4d43ts61(v=vs.90).aspx
Commenting here in case the mod queue is still not working. Warning, rant follows
I WILL NOT APPROVE ANOTHER QUESTION FRO$$anonymous$$ YOU IDENTICAL TO THIS ON$$anonymous$$ I WILL $$anonymous$$ETE ANY ANBODY ELSE APPROVES.
Get the message? Take the time to learn the basics of OOP and value versus reference types. Go to the beginner section and do the tutorials. Please don't post new questions unless you have implemented the advice given in previous answers.
Rant over. I do wish you the best of luck in your coding efforts and making a game.
Answer by Uldeim · Nov 21, 2014 at 01:41 AM
No, bools are VALUE types, just like ints were the last time you asked this question. Your Get variable strategy will never work for value types. This is really basic stuff, and if you don't get it (which is cool, we were all new once) you really really REALLY need to read some stuff and get it pounded into your head. It's important.
Instead, you could do something like:
private Follower Follower_Human;
Follower_Human = HumanFollower.GetComponent<Follower>();
Follower_Human.Following = true;
Your approach itself will, technically, work if you do it like that, but it'll be kind of a nightmare to maintain. You'll need x^2 if statements. One way you could help that is to realize that you don't actually care if you're setting a false value back to false. eg.
if (Follower_Human.Following)
{
Follower_Alien.Following = false;
Follower_Robot.Following = false;
Follower_Monster.Following = false;
}
This is better, but you're still going to be duplicating code. If you don't have any triggers on Following changing state, you could move on to:
if (Follower_Human.Following)
{
SetAllFollowersToUnfollow();
Follower_Human.Following = true;
}
Really, though, you shouldn't be doing this in Update at all, you should be doing this when, and only when, a new follower starts following. Update is probably the worst place to put code, but most new coders put everything there. Something to watch out for.
A dissertation on event driven programming or delegates and events in C# is well beyond the scope of this QA, but you may want to look into them.
A far better strategy, without requiring delegates and events, would be to simply store a reference to the following character.
Follower currentFollower;
void ChangeFollower (GameObject newFollower){
currentFollower.following = false;
currentFollower = newFollower.GetComponent<Follower>();
currentFollower.following = true;
}
This assumes only one follower is allowed. For multiple followers you could follow the same strategy with a list.
Far superior indeed. That's basically what I was saying with the whole "not in Update" thing. Do that.
the only question i have is what do i do with the variable current follower?
Here's a brief rundown of value types versus reference types.
Value types include int and bool and all of the other primitives, and structs. These are copied into a new variable every time you make an assignment. So in your code above your HumanFollowerStateGet is a different bool to the one on your Follower script. If you want to access the bool on the Follower script then you must access the instance of the Follower every time.
Reference types include all classes. Every time you make an assignment you get a reference to the original object. So if you modify a variable of Follower type, you change the original instance of the Follower script.
Does that help at all?
Answer by Kiwasi · Nov 22, 2014 at 05:02 AM
Here is some more code. I am determined to help you get this. Making it an answer, because its long enough to stand by itself. As written it will change the follower every two seconds. But you get the idea.
// This should be present on every follower
public class Follower : MonoBehaviour {
public bool isFollowing;
void Update (){
if(isFollowing){
Debug.Log("I'm Following :" + gameObject.name);
}
}
}
// This should exist once and only once
public class FollowerManager : MonoBehaviour {
private Follower currentFollower;
public void ChangeFollower (GameObject newFollower){
if(currentFollower){
currentFollower.isFollowing = false;
}
currentFollower = newFollower.GetComponent<Follower>();
currentFollower.isFollowing = true;
}
// Some random code to change the follower. Change to suit your game
private float timer = 0;
private int index = 0;
// Add the required number of followers via the inspector
public GameObject[] followers;
void Update (){
timer += Time.deltaTime;
if (timer > 2){
ChangeFollower (followers[index]);
timer = 0;
index ++;
if(index >= followers.Length) index = 0;
}
}
}
Edit: Since you are having trouble implementing this here is a Unity Package that contains an implementation of my scripts. You are free to use however you like.
By the way why did you do private follower current follower? I dont see any definition to follower
Everything should be private unless there is a good reason to make it public. I had no reason, so I wrote private.
If I was writing for myself I would have used private for all of the variables except isFollowing, and tagged the ones I wanted in the inspector with [SerializeFeild]. I also would have used custom properties on currentFollower, giving it a public get and a private set.
However at your stage these are niceties, rather then essentials. Lets get your code working first before we worry too much about getting the finer details sorted.
Hope you sleep well. Let me know how it goes in the morning.
Here is my one follower code. it caused them to both follow me. Theres too many characters on the followers' scripts so i added a Facebook link, if you have a Facebook this is my page, at the top of the page is the script post.https://www.facebook.com/aidanpallian/posts/709909602438244
using UnityEngine;
using System.Collections;
public class OneFollower : $$anonymous$$onoBehaviour {
private GameObject HumanFollowerGet;
private GameObject AlienFollowerGet;
public GameObject CurrentFollowerGet;
public bool HumanFollowerStateGet;
public bool AlienFollowerStateGet;
public bool CurrentFollowerStateGet;
void Start () {
HumanFollowerGet = GameObject.Find ("Human Follower");
AlienFollowerGet = GameObject.Find ("Alien Follower");
}
void Update () {
HumanFollowerStateGet = HumanFollowerGet.GetComponent <HumanFollower> ().Following;
AlienFollowerStateGet = AlienFollowerGet.GetComponent <AlienFollower> ().Following;
if (HumanFollowerStateGet == true)
{
ChangeFollower (HumanFollowerGet);
}
if (AlienFollowerStateGet == true)
{
ChangeFollower (AlienFollowerGet);
}
}
void ChangeFollower (GameObject NewFollower) {
if (CurrentFollowerGet)
{
CurrentFollowerStateGet = false;
}
CurrentFollowerGet = NewFollower;
CurrentFollowerStateGet = true;
}
}
Your strategy of storing a remote bool locally cannot work. You need to store the Follower component, and use follower.Following. Your code still deneonstrates a fundamental misunderstanding if value types.
This new code is simply a rehash of your existing code, and bears no resemlance to my code. Try scrap your entire code and copying in $$anonymous$$e.
Your use of set and get in your variable names is also against convention.
Your answer
Follow this Question
Related Questions
Game Works on Computer, but not on iPhone 0 Answers
Main Menu scrpit not working 1 Answer
Unity 3d android not working 1 Answer