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;
}
}
}