Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 05, 2017 at 02:48 AM by JFGames for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JFGames · Jun 09, 2017 at 06:43 AM · scripting problemstringnot workingno error

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges