Question by
painfulfishboy · Mar 10, 2021 at 09:50 PM ·
referencing
How to reference a class repeadedly ,How to update a reference?
I've been making a puzzle game that uses states to declare if a puzzle is complete, however, I can't find out how to reference the states in the other script repeatedly to update them when the player wants to move. more detail is below if anyone could help that would be great
public class Character_Controller : MonoBehaviour
{
//Control Values
public float CharacterSpeed = 5f;
public float JumpHeight = 5f;
public float Gravity = 5f;
public float GroundDistance = 0.5f;
public LayerMask Platform;
public CharacterController PlayerCharacter;
private GameObject EventManager;
//States for puzzle
private bool movePuzzleState;
private bool jumpPuzzleState;
private bool endPuzzleState;
private void Awake()
{
PlayerCharacter = GetComponent<CharacterController>();
}
IEnumerator CheckForStates()
{
EventManager = GameObject.Find("EventManager");
endPuzzleState = gameObject.GetComponent<End_Puzzle>().EndPuzzleState;
movePuzzleState = gameObject.GetComponent<Movement_Puzzle>().MovementPuzzleState;
jumpPuzzleState = gameObject.GetComponent<Jump_Puzzle>().JumpPuzzleState;
Debug.LogError("this is the IEnumerator");
yield return null;
}
private void Start()
{
StartCoroutine(CheckForStates());
}
//This fetches the objects from the other codes I am referencing.
private void Update()
{
if(!movePuzzleState)
{
StartCoroutine(CheckForStates());
//movePuzzleState = GetComponent<Movement_Puzzle>().MovementPuzzleState;//gameObject.GetComponent<Movement_Puzzle>().MovementPuzzleState;
}
else if(movePuzzleState)
{
Vector2 movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
PlayerCharacter.Move(movement * Time.deltaTime * CharacterSpeed);
}
else
{
Debug.LogError("Movement puzzle not working");
}
}
below is the code I am referencing
[SerializeField] public bool MovementPuzzleState;
public void Awake()
{
MovementPuzzleState = false;
}
}
Comment
Your answer

Follow this Question
Related Questions
How select between diferent variables from a referenced script with one variable 2 Answers
Copyright Code and Citing 0 Answers
Reference problems to other Gameobjects 2 Answers
Change NavMeshAgent Speed With Script? 1 Answer
NullReferenceException: Object reference not set to an instance of an Object 1 Answer