- Home /
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!
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
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
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
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.
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!!!
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.
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.
Thanks for the reply! All the tags were set and I switched the World.Start with awake and I still have the same nullref.
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.
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.
Now wait a second what is this line or where is it? ally = GameObject.FindGameObjectWithTag("World").GetComponent().currentCharacter = characters[charactersIndex];
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.
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"
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!
$$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
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.
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.
Your answer