- Home /
The question is answered, right answer was accepted
Problem changing string value.
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;
}
}
}
Answer by JFGames · Jun 05, 2017 at 02:47 AM
Nevermind, I changed the script entirely now using a set of booleans instead of strings and having a second assistant script specifically for switching directions and it ended up working.
Answer by JFGames · Jun 04, 2017 at 09:31 PM
After further research I have found that the entire if (IsComplete == true) statement is not working. This confuses me even more now.
Follow this Question
Related Questions
Door Script compiler error 0 Answers
Text component change through script not working 1 Answer
If two PlayerPrefs are on? 2 Answers
NullReferenceException when adding String to list 1 Answer
Multiplying Vector2 from another script. 2 Answers