Hi, my game has a computer with a script attached to it. The script loads a mini-game, once the mini-game is complete (decided by an assistant script) it runs one of two functions, It will either open a door or change the direction of a conveyor belt. The direction of the Conveyor Belt is decided through a string variable: “Left” for left, or “Right” for right. Everything works in the computer script except changing the String value for the conveyor belt. I’m not getting any error messages. I ran some tests and determined that the part that doesn’t work is the two if statements toggling the string.
Thanks in advance to whoever can help me!
/ Full scripts below /
The Computer Script (Changes the string):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Computer : MonoBehaviour {
public bool IsComplete;
private bool PlayerInBox;
public bool ChangesBelt;
private bool HasChangedBelt;
public bool OpensDoor;
private bool HasOpenedDoor;
public PlayerController Player;
private ComputerAssistant Assistant;
public ConvBelt Belt;
public string Scene;
public Animator Door;
public bool Conv;
void Start() {
Player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
Assistant = FindObjectOfType<ComputerAssistant>();
if (!IsComplete & PlayerInBox & Input.GetKeyUp(KeyCode.Return))
{
Application.LoadLevelAdditiveAsync(Scene);
Player.IsCutscene = true;
}
if (Assistant.IsComplete)
{
IsComplete = true;
Player.IsCutscene = false;
}
if (IsComplete == true)
{
if (OpensDoor & !HasOpenedDoor)
{
Door.SetBool("Open", true);
HasOpenedDoor = true;
}
if (ChangesBelt == true & !HasChangedBelt == false)
{
if (Belt.Direction == "Left") // <--------|
{
Belt.Direction = "Right";
HasChangedBelt = true; // These don't work!
}
if (Belt.Direction == "Right") // <-------|
{
Belt.Direction = "Left";
HasChangedBelt = true;
}
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
{
PlayerInBox = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
{
PlayerInBox = false;
}
}
}
The Assistant Script (Decides if the mini-game is complete):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ComputerAssistant : MonoBehaviour {
public bool IsComplete;
public string Scene;
// Update is called once per frame
void Update () {
if (IsComplete)
{
StartCoroutine(UnloadPuzzle());
}
}
private IEnumerator UnloadPuzzle ()
{
yield return new WaitForSeconds(2);
SceneManager.UnloadSceneAsync(Scene);
}
}
The Conveyor Belt Script (Has the string):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConvBelt : MonoBehaviour {
public string Direction;
public GameObject Player;
private bool PlayerInBox;
// Use this for initialization
void Start () {
Player = FindObjectOfType<PlayerController>().gameObject;
}
// Update is called once per frame
void Update () {
if (PlayerInBox == true)
{
if (Direction == "Left")
{
Player.transform.Translate(-0.1f, 0, 0);
}
if (Direction == "Right")
{
Player.transform.Translate(0.1f, 0, 0);
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
{
PlayerInBox = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
{
PlayerInBox = false;
}
}
}