Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 bicomicguy · Dec 09, 2018 at 07:21 PM · aireferencingfollow player

Trying to refrence my selected character for AI follow, I'm stumped.

So I have a script that switches character control between a team of four, and I want all the other characters to follow the selected character. I made a script for the AI follow, but I need to reference the character switch script to find the current character. The character switch script is:

     public class World : MonoBehaviour {
     
       public GameObject[] characters;
       public GameObject currentCharacter;
       public int charactersIndex;
     
         void Start () {
         InitializeCharacters();
         }
     
         void InitializeCharacters(){
         charactersIndex = 0;
         currentCharacter = characters[0];
         characters[1].GetComponent<PlayerMovement>().enabled = false;
         characters[2].GetComponent<PlayerMovement>().enabled = false;
         characters[3].GetComponent<PlayerMovement>().enabled = false;
         GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraFollow>().target = characters[charactersIndex].transform;
         }
         
         void Update () {
     
           if(Input.GetKeyDown("space")){
               switchCharacter();
           }        
         }
     
         void switchCharacter(){
          charactersIndex++;
          if(charactersIndex == characters.Length){
          charactersIndex = 0;
         }
          currentCharacter.GetComponent<PlayerMovement>().enabled = false;
          characters[charactersIndex].GetComponent<PlayerMovement>().enabled = true;
          GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CameraFollow>().target = characters[charactersIndex].transform;
          currentCharacter = characters[charactersIndex];   
         }
     }
 

I want to get the currentCharacter from this script for the follow script and I thought it would work if I used this line of code in it: ally = GameObject.FindGameObjectWithTag("World").GetComponent<World>().currentCharacter = characters[charactersIndex]; But Unity gives me the error "NullReferenceException: Object reference not set to an instance of an object AllyController.Start ()" when I use this line. I posted the entire character switch code because maybe I'm referencing the wrong thing. Thanks in advance!

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mansoor090 · Dec 11, 2018 at 10:33 AM

public GameObject MainPlayer;

when switching player , assign main player the switched player, give reference to the AI by inheritance if AI has seperate Follow script

Comment
Add comment · Show 3 · 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 nicholasw1816 · Dec 11, 2018 at 10:43 AM 0
Share

He forgot to reference the World script when assigning (characters[charactersIndex];. The code is in AllyController.Start (), but the GameObject variable belongs to the World script;

 ally = GameObject.FindGameObjectWithTag("World").GetComponent<World>().currentCharacter = characters[charactersIndex];//<---No reference to script

avatar image mansoor090 nicholasw1816 · Dec 11, 2018 at 10:49 AM 0
Share

may be you are right , but i am still unable to get his confusion. Once he has the player reference , he can update the follow target in update function

avatar image nicholasw1816 mansoor090 · Dec 12, 2018 at 03:12 AM 1
Share

You were right, "reference to the AI by inheritance if AI has seperate Follow script" I just pointed out the exact code for when he stumbles across your post, and gave you proper thumbs up for finding it.

avatar image
1

Answer by bicomicguy · Dec 11, 2018 at 10:41 PM

I really appreciated the input guys, this project is a bit of a pain, but I'm really grateful for your help!!!

Comment
Add comment · Show 1 · 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 nicholasw1816 · Dec 12, 2018 at 03:22 AM 1
Share

Hey Gl with you project man, your doing a nice job, things get hard sometimes for me as well being 1 year in to a project. It's usually little things like this in my scripts. $$anonymous$$eep your head up! and ask me if you need help in the future.

avatar image
0

Answer by Nocktion · Dec 09, 2018 at 07:50 PM

First of all make sure that all tags are set on the gameobjects. Then you should try to replace World.Start with an Awake, because Awake gets called before Start. Therefore maybe the problem is that AllyController.Start gets called before World.Start and since the currentCharacter is not initialized when you try to access it, it just simply throws a nullref.

Comment
Add comment · Show 5 · 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 bicomicguy · Dec 09, 2018 at 11:02 PM 0
Share

Thanks for the reply! All the tags were set and I switched the World.Start with awake and I still have the same nullref.

avatar image Nocktion · Dec 10, 2018 at 10:03 PM 0
Share

Then first check what is null a, GameObject.Find...Tag("World") b, The World component c, characters array d, Characters[index] If we'd know what is null then it would be much easier to solve.

avatar image bicomicguy Nocktion · Dec 10, 2018 at 10:08 PM 0
Share

It says .currentCharacter = characters[charactersIndex] is the problem, I changed it to GameObject.FindGameObjectWithTag("World").GetComponent().characters[0] and it works, but it only follows the first character.

avatar image Nocktion · Dec 11, 2018 at 06:44 AM 0
Share

Now wait a second what is this line or where is it? ally = GameObject.FindGameObjectWithTag("World").GetComponent().currentCharacter = characters[charactersIndex];

avatar image bicomicguy Nocktion · Dec 11, 2018 at 10:38 PM 0
Share

On the AllyController script, I also tried to make a reference for if the Player$$anonymous$$ovement was enabled follow that character, but that didn't work. I think I may have to find a new way to switch characters individually and then define the ally in the new script.

avatar image
0

Answer by nicholasw1816 · Dec 10, 2018 at 02:57 PM

You never defined your character array. you must first do that.

 characters[0].gameObject = "the gameobject you want here"
 characters[1].gameObject = "the gameobject you want here"
 characters[2].gameObject = "the gameobject you want here"
 characters[3].gameObject = "the gameobject you want here"


Comment
Add comment · Show 7 · 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 bicomicguy · Dec 10, 2018 at 09:19 PM 0
Share

I have them defined in the inspector, I have 10 selectable characters will that rule them out if I script just my currently loaded characters? I'm pretty new to coding btw. Thanks for replying!

avatar image nicholasw1816 bicomicguy · Dec 11, 2018 at 10:09 AM 0
Share

$$anonymous$$y bad, try this my friend, apply the new Character to both variables separately. Also, whe you tried to assign the char, you are referencing (characters[charactersIndex];) outside of the script it belongs to, you have to reference the script first, then the GameObject, the same way you did with current character.

 Gameobject newAlly = GameObject.FindGameObjectWithTag("World").GetComponent().characters[charactersIndex];
 
 GameObject.FindGameObjectWithTag("World").GetComponent().currentCharacter = newAlly;
 
 ally = newAlly






avatar image bicomicguy nicholasw1816 · Dec 11, 2018 at 10:40 PM 0
Share

This worked, but the characters still only follow the first character. I'm going to try a new way to switch them individually from the index and maybe make a selected component and the one who is selected will be the ally.

Show more comments
avatar image
0

Answer by FBP_SHH · Dec 24, 2018 at 09:15 AM

Instead of doing .GetComponent().enabled = false; add a public bool to PlayerMovement called selected or leading.

Then do GetComponent().leading = true; instead.

Comment
Add comment · 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

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

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Running and Follow at Certain Distance 0 Answers

Follower Airplane problem 1 Answer

enemy flying through air when targeting player 1 Answer

i want my ai to follow my player on both axis but he only follows on one,Transform only moves on one axis 0 Answers

Enemy follows player but when enemy is hit it doesnt respawn 3 Answers


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