Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by awplays49 · Nov 20, 2014 at 11:19 PM · notworking

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awplays49 · Nov 20, 2014 at 10:46 PM 0
Share

By the way the bools are programmed to get the state of other bools that represent whether that follower is following or not

avatar image Kiwasi · Nov 21, 2014 at 02:50 AM 0
Share

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

avatar image Kiwasi · Nov 22, 2014 at 03:27 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awplays49 · Nov 21, 2014 at 01:44 AM 0
Share

Thanks! :)

avatar image Kiwasi · Nov 21, 2014 at 02:58 AM 1
Share

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.

avatar image Uldeim · Nov 21, 2014 at 03:26 AM 0
Share

Far superior indeed. That's basically what I was saying with the whole "not in Update" thing. Do that.

avatar image awplays49 · Nov 22, 2014 at 04:13 AM 0
Share

the only question i have is what do i do with the variable current follower?

avatar image Kiwasi · Nov 22, 2014 at 04:14 AM 0
Share

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?

Show more comments
avatar image
0

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.

Comment
Add comment · Show 22 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awplays49 · Nov 22, 2014 at 05:14 AM 0
Share

By the way why did you do private follower current follower? I dont see any definition to follower

avatar image Kiwasi · Nov 22, 2014 at 05:24 AM 0
Share

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.

avatar image awplays49 · Nov 22, 2014 at 01:33 PM 0
Share

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;
     }
 }
avatar image Kiwasi · Nov 22, 2014 at 07:03 PM 0
Share

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.

avatar image Kiwasi · Nov 22, 2014 at 07:06 PM 0
Share

Your use of set and get in your variable names is also against convention.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Lights only work when I look at the ground. 0 Answers

OnCollisionExit2D not getting called? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges