0

I am working on a game on Unity2D with different characters on screen. A big part of the game is switching between these characters to solve puzzles, as different characters can solve certain puzzles. I have a public bool in each script for the different characters which tells the script whether that character is active or not, to determine whether you can control them. My issue occurs when I try to assign the different scripts I need to my main script. I have three different game objects containing scripts I need values from, but I am unsure about how I would be able to do that. The three scripts I need are inside GameObjects in the hierarchy, I am a bit stuck as I can't assign their scripts to my main class.

Here is the code for my main script incase it helps:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainScript : MonoBehaviour
{
    public Movement_Leader leaderScript; //The three public values for the scripts
    public Movement_Jumper jumperScript;
    public Movement_Dasher dasherScript;

    // Start is called before the first frame update
    void Start()
    {
        leaderScript = GetComponent<Movement_Leader>(); //The values are assigned a null value when the Start() method is called

        jumperScript = GetComponent<Movement_Jumper>();

        dasherScript = GetComponent<Movement_Dasher>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire2"))
        {
            ChangeCharacter();
        }
    }

    void ChangeCharacter()
    {
        print(leaderScript.leaderActive);
        print(jumperScript.jumperActive);
        print(dasherScript.dasherActive);
        if (leaderScript.leaderActive == true)
        {
            leaderScript.leaderActive = false;
            jumperScript.jumperActive = true;

        }
        else if (jumperScript.jumperActive == true)
        {
            jumperScript.jumperActive = false;
            dasherScript.dasherActive = true;
        }
        else if (dasherScript.dasherActive == true)
        {
            leaderScript.leaderActive = true;
            dasherScript.dasherActive = false;
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • I mean no disrespect, however you are aware that we do not see the line numbers you are talking about. Can you be more specific. – JohnG Feb 15 '21 at 03:06
  • Yes I realized that shortly after posting that I made that mistake, I apologize. The three values I printed in my method ChangeCharacter() are the source of my error. While leaderScript.leaderActive works fine the error comes from both jumperScript.jumperActive and dasherScript.dasherActive. – KingExodia Feb 15 '21 at 03:14
  • 1
    The only objects that “could” be `null` in the… `print(jumperScript.jumperActive);` … lines of code are… `leaderScript`, `jumperScript` and/or `dasherScript.` This assumes `print(null)` will not throw an exception. I suggest you trace those variables starting at the `Start()` code. – JohnG Feb 15 '21 at 03:41
  • I did some debugging and found that the issue was caused because the leaderScript, jumperScript, and dasherScript are null when I get them in the Start() method. I can't assign the scripts to the public values I made in the MainScript in the inspector menu though. I can assign one script properly if I add the MainScript as a component of one of my characters but otherwise I don't know how to set it up so I can assign all three scripts correctly. For some reason I didn't do any debugging there before, but that still leaves me with another problem. – KingExodia Feb 15 '21 at 03:51
  • I am not that familiar with unity3d, however, it is usually good practice to “check” for these `null` objects “before” you try to use them. Considering the fact that the code crashes when those variables are `null`, then it would be wise to “CHECK” to see if they are `null` BEFORE you call the `print` and the other code. Example… `if(leaderScript != null) { call print etc.. }` – JohnG Feb 15 '21 at 03:57
  • Yes I know, I will admit I was a bit lazy with my checks and I could've found out the objects were null earlier. But, I am still left with the issue that I am unable to assign the scripts to the public values in the inspector tab and I am unsure what the best way to do that is, because I have 3 different GameObjects with the leaderScript, jumperScript, and dasherScript. – KingExodia Feb 15 '21 at 04:11
  • Well, you are getting off base from your original question. Given the limited code, it is clear, that if the `ChangeCharacter` code is called AND those variables are `null`… your code is going to crash. You should check this anyway JUST to prevent the crash. There are no guarantees the variables will never be `null.` I suggest you add the `null` checks and run the code to help debug other code that may need the same kind of check. – JohnG Feb 15 '21 at 04:34
  • Again, I am not familiar with unity3d, so I will not be much help there. You commented that you were… _”unable to assign the scripts to the public values in the inspector tab and I am unsure what the best way to do that is, because I have 3 different GameObjects with the leaderScript, jumperScript, and dasherScript”_ … It may help to edit your question along those lines. – JohnG Feb 15 '21 at 04:34
  • Alright. Thanks for the assistance! – KingExodia Feb 15 '21 at 04:40
  • [`GetComponent`](https://docs.unity3d.com/ScriptReference/Component.GetComponent.html) searches that component on the **same** GameObject this `MainScript` is attached to => If this script is not attached to the same GameObject as all three components you are trying to get there, then it will of course return `null`. If you are anyway dragging these in via the Inspector, then there is no need to use `GetComponent` at all! – derHugo Feb 15 '21 at 07:32
  • This proved useful. I am not too experienced with C# or Unity as it's not my first game engine so that helped me understand GetComponent. I ended up creating an empty GameObject to parent the characters to, and I added my MainScript as a component on the Gameobject. Then I added a public GameObject for each of my characters to assign in the inspector and I was able to get the scripts with no issue using the Start() method. Everything is working now, so thank you. – KingExodia Feb 15 '21 at 18:37

0 Answers0