Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Same variable across two different scripts.

Discussion in 'Scripting' started by BobRos, Oct 12, 2015.

  1. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    I'm pretty new to scripting, and I have a question about variables.
    If I have a bool variable in one script, is there a way for me to reference/access from another script?

    I'm asking because I have two scripts. One is a simple cursor lock script (I'll call it Script 1)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CursorLock : MonoBehaviour
    5. {
    6.  
    7.     void Start()
    8.     {
    9.         Cursor.lockState = CursorLockMode.Locked;
    10.         Cursor.visible = false;
    11.     }
    12.     void Update()
    13.     {
    14.         Cursor.lockState = CursorLockMode.Locked;
    15.         Cursor.visible = false;
    16.     }
    17. }
    18.  
    The other is basically a script that makes a dialog screen pop up when you press "e" and are near the person. (Script 2)

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class DoorScript : MonoBehaviour {
    7.  
    8.  
    9.    // Use this for initialization
    10.    void Start () {
    11.  
    12.   }
    13.  
    14.   // Update is called once per frame
    15.   void Update()
    16.   {
    17.   bool lockOutOf;
    18.  
    19.   float mainChar;
    20.   mainChar = GameObject.Find("MainChar").transform.position.x;
    21.  
    22.   if ((mainChar <= -153.6f & mainChar >= -771.6f) & (Input.GetKey(KeyCode.E)))
    23.   {
    24.  
    25.  
    26.   GameObject.Find("DialogScreen").transform.localScale = new Vector3(1, 1, 1);
    27.   lockOutOf = false;
    28.  
    29.   }
    30.  
    31.   if ((mainChar > -153.6f & mainChar < -771.6f))
    32.   {
    33.   GameObject.Find("DialogScreen").transform.localScale = new Vector3(0, 0, 0);
    34.   lockOutOf = true;
    35.   }
    36.  
    37.  
    38.   }
    39. }
    40.  

    Is there anyway I could reference lockOutOf (the bool in script 2) and use it in Script 1, so that when lockOutOf = true, the cursor is locked, and when lockOutOf = false, the cursor is unlocked?

    Sorry if this didn't make much sense. If it didn't, my question is basically how can I make the first if statement in script 2 unlock the cursor, and the second if statement lock the cursor.
     
  2. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Yes, just make it
    Code (csharp):
    1.  
    2. public bool lockOutOf;
    3.  
    Then you can literally just type
    Code (csharp):
    1.  
    2. lockOutOf = false;
    3.  
    In your other script :)
     
  3. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Also,
    In Script1, ii'd remove the code in Start as it's already in Update to avoid confusion :)
     
  4. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Ok, I'll try that. thanks for the help!
     
  5. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102

    You're probably going to require
    Code (csharp):
    1. DoorScript.lockOutOf = false;
    in the other script actually :p
     
  6. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Okay, so in script #2 I changed the "bool lockOutOf" to "public bool lockOutOf", and I got about 100 compile errors.
    Then I tried moving "public bool lockOutOf" to the 7th line, under "public class DoorScript : MonoBehaviour {"
    This made the compile errors go away, but then when I tried using it in script #1. The code I wrote is below.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CursorLock : MonoBehaviour
    5. {
    6.  
    7.  
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.     void Update()
    13.     {
    14.  
    15.         Cursor.lockState = CursorLockMode.Locked;
    16.         Cursor.visible = false;
    17.  
    18.         if (lockOutOf = false)
    19.         {
    20.             Cursor.lockState = CursorLockMode.None;
    21.         }
    22.         if (lockOutOf = true)
    23.         {
    24.             Cursor.lockState = CursorLockMode.Locked;
    25.         }
    26.  
    27.     }
    28. }
    29.  
    But when I do this, I get two errors, one for each time I use lockOutOf. They say that "the name lockOutOf does not exist in the current context. So what am I doing wrong now? lockOutOf is a public variable, so script 1 should be able to use it, right??
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    @eclolev's script isn't quite as straightforward as he's making it seem. In order to access script 1 from script 2 you need a reference to it, and he doesn't do that. @Jamster is closer, but the syntax needs to be: "public static bool lockOutOf;" in your cursor script, and then any script can access it using "CursorLock.lockOutOf = true;". A note of caution: this is usually only a good way to go about it if there is one and only one of the thing in the scene. Cursor lock out is exactly such a situation, so it's good for this case. Incidentally, it would also be good for your dialog box - more on that in a second.
    (edit: there's a lot more, but I wanted to post this immediately since there's still this whole conversation with bad advice happening as I type it)
     
    BobRos likes this.
  8. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Ahhh yeah... It's one of those days :oops:
     
  9. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Ok, I followed your suggestions and it stopped the compiling errors, but the script just doesn't work now. So if you don't mind, I'll just post the whole script, and you can tell me how to fix it if you can.

    Script 1 (CursorLock): Supposed to lock the cursor, and then lock/unlock it depending on whether lockOutOf is true or false.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CursorLock : MonoBehaviour
    5. {
    6.  
    7.     public static bool lockOutOf;
    8.  
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.     void Update()
    14.     {
    15.  
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         Cursor.visible = false;
    18.  
    19.         if (lockOutOf = false)
    20.         {
    21.             Cursor.lockState = CursorLockMode.None;
    22.         }
    23.         if (lockOutOf = true)
    24.         {
    25.             Cursor.lockState = CursorLockMode.Locked;
    26.         }
    27.  
    28.     }
    29. }
    30.  
    Script 2 (DoorScript): Is supposed to make a GUI appear if the player is in a certain area (near the person that they're talking to), and pressed the "e" key. I also supposed to set lockOutOf to true or false, depending on whether or not the player is near the person they're talking to or not.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class DoorScript : MonoBehaviour {
    6.  
    7.     public static bool lockOutOf;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13. }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.  
    20.         float mainChar;
    21.         mainChar = GameObject.Find("MainChar").transform.position.x;
    22.  
    23.         if ((mainChar <= -153.6f & mainChar >= -771.6f) & (Input.GetKey(KeyCode.E)))
    24.         {
    25.  
    26.  
    27.             GameObject.Find("DialogScreen").transform.localScale = new Vector3(1, 1, 1);
    28.             CursorLock.lockOutOf = false;
    29.  
    30.         }
    31.  
    32.         if ((mainChar > -153.6f & mainChar < -771.6f))
    33.         {
    34.             GameObject.Find("DialogScreen").transform.localScale = new Vector3(0, 0, 0);
    35.             CursorLock.lockOutOf = true;
    36.         }
    37.  
    38.  
    39.     }
    40. }
    41.  
    I don't know if this matters or not, but here's what's in the actual game. Hopefully this won't overcomplicate things, if it does just ignore it.
    I have the player, who's called "MainChar". he's just a sprite with a 2d collider, a movement script, and a rigidbody. The CursorLock script is also attached to him, since I didn't know where to put it. The Main Camera is a child of his, so that it follows him around.
    I have two seperate canvases, one is in world space and contains an image that when clicked, makes another image appear.
    The GUI that appears is a child of the second canvas, which is in the screen space. The child of this, the GUI that appears, is an image called "DialogScreen"
    Hopefully this helps a little, if not just ignore it.


    Again, I'm sorry if my coding is just completely wrong, or if I'm missing some super basic stuff, but I just started coding a week or two ago. So if you guys could help me, I'd really appreciate it. And sorry for the long posts. I'm not great at explaining this stuff.
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    (continued from my last post) The static variable will work just fine as long as there is one and only one door in your game. This seems like an unlikely proposition. :) However, let's say you add a second door. Do you want them to be fighting over whether the cursor should be locked or not? Because that'll happen.

    First, let's talk about those numbers in your door script. That's called "hard-coding", and it's bad. It guarantees that you won't be able to re-use that door script for any other door. So, let's replace those numbers with public floats that you will be able to modify in the door's Inspector view:
    Code (csharp):
    1. public float minInteractionPositionX = -771.6f;
    2. public float maxInteractionPositionX = -153.6f;
    3.  
    4. ....
    5. if (mainChar >= minInteractionPositionX && mainChar <= maxInteractionPositionX && Input.GetKey(KeyCode.E) ) {
    You should also rename mainChar to a more descriptive name like mainCharXPosition, but I'm not gonna do that just now (you'll see why towards the end of the post).

    Instead, I think you should use the concept of separating your GUI from your main functionality. The general principle is this: Your in-game objects should not be aware that the GUI even exists. They expose the information the GUI needs, but all the work that the GUI needs is done by the GUI, not by the objects. If you completely scrap your GUI and rebuild it, you should ideally not be required to change a single line of your in-game objects' code in order to do so.

    So how can we make the GUI behave correctly in this scenario? By having it keep a reference to the player. In this case, we can use something called a singleton. So you now know how you can have a static variable, and you cna have variables that point to objects? Let's combine those concepts, and have a static variable that points to the one instance of the object in the scene:
    Code (csharp):
    1. public static Player playerInstance;
    2. void Awake() {
    3. playerInstance = this;
    4. }
    Put this code in the script that controls your player. Now, anytime any script wants to do anything with your player object, you can access it using Player.playerInstance. An example will come in a second.

    So what does this have to do with your door and cursor lock-out? Well, when your player comes across a door, right now your door directly modifies the UI. But, conceptually, what we want the UI to be displaying is "Whatever the player is interacting with right now." So we want the player itself to know what it's interacting with right now. And that is the door will modify.

    So let's add a public variable to the Player script:
    Code (csharp):
    1. public DoorScript interactingWithDoor = null;
    Most of the time, this variable will be null, meaning the player is not interacting with a door. However, when the player walks up and presses E, the door will tell the player, "hey, player, you're interacting with me now!":
    Code (csharp):
    1. if (Player.playerInstance.transform.position.x >= minInteractionPositionX && Player.playerInstance.transform.position.x <= maxInteractionPositionX && Input.GetKey(KeyCode.E) ) {
    2. Player.playerInstance.interactingWithDoor = this;
    3. }
    4. if (Player.playerInstance.interactingWithDoor == this && (Player.playerInstance.transform.position.x < minInterationPositionX || Player.playerInstance.transform.position.x > maxInteractionPositionX) ) {
    5. Player.playerInstance.interactingWithDoor = null;
    6. }
    (Being able to access the component itself is the strength of a singleton over just static variables. That's how we're able to access the Transform attached to it, and get its position directly.)

    So what this does: If the player walks up to the door and presses E, the door tells the player "Hey! It's me! I'm the one you're interacting with!". If the player then walks away from that door, the door sets this to null, but only if that same door is the one the player was interacting with (which is the reason to do this instead of a simple bool). If some other door was being interacted with, it'll leave it alone.

    ----

    So now that we have well-structured data, we are finally ready to hook the GUI into it. On your cursor locking code, you can simply put this:
    Code (csharp):
    1. if (Player.playerInstance.interactingWithDoor != null) {
    2. //cursor is locked
    3. }
    Now your GUI is responsible for your GUI, and the player script doesn't need to worry about it. More importantly, the doors don't need to fight over it. Your dialog screen should have the same logic on it, and make itself appear and disappear. Bonus: Your door scripts can even have your own variables on it that your dialog box can easily access (e.g. a customizable message that appears that's different for every door you open) - simply make that a public string variable on the door, and your dialog box can access it like so:
    Code (csharp):
    1. someTextObject.text = Player.playerInstance.interactingWithDoor.customMessage;
     
    BobRos likes this.
  11. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    I'm in the process of reading your whole post, and changing the code according to it. Thank you so much for the help, I really appreciate it. I think I might be able to get the script to work now, so thank you.
     
  12. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48

    And just to be clear, in your post when you say "Player", should I change that to the name of MainChar's movement script? Or is that referencing the actual player (in which case I should replace it with "MainChar")?
    Or should I just leave it as Player? When I did that (left it as Player) I got a ton of compile errors, so I'm just wondering what that is referring to.

    [edit]: Here are all the names of the objects in the game, if it helps any.

    The sprite that the player controls is called "MainChar". His movement script is called "PlayerMovement". The canvas that is in the object space, and holds the door that you press "e" near to get the dialog, is called "InGameChat". It's child is an image called "Door", which is supposed to make the dialog appear when you press "e" near it. The canvas that contains the actual dialog is called "FullScreenGUI", and it's in the screen space. It's child is "DialogScreen", which is an image that is supposed to appear when you press "e" near "Door". Again, sorry if this confused, just trying to help.
     
    Last edited: Oct 12, 2015
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Whatever the name of the script that is on your main character - looks like it should be PlayerMovement.
     
    BobRos likes this.
  14. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Sorry to keep bothering you, I probably shouldn't have tried to write something this difficult so early on. But everything besides the door script is compiling. The door script has errors pretty much all over this part:
    Code (CSharp):
    1.        if (PlayerMovement.playerInstance.transform.position.x >= minInteractionPositionX && PlayerMovement.playerInstance.transform.position.x <= maxInteractionPositionX && Input.GetKey(KeyCode.E) )
    2.         {
    3.         PlayerMovement.playerInstance.interactingWithDoor = this;
    4.         }
    5.         if (PlayerMovement.playerInstance.interactingWithDoor == this && (PlayerMovement.playerInstance.transform.position.x<minInterationPositionX || PlayerMovement.playerInstance.transform.position.x> maxInteractionPositionX) )
    6.         {
    7.         Player.playerInstance.interactingWithDoor = null;
    8.         }
    Any idea what's wrong here? This honestly has gotten to complicated for me to understand. I'm not really sure where to even start editing it to make it work, so I might just start from scratch with all the new stuff, and try and write it that way. But if you have anything that could help me, I'd be glad to hear it.
     
  15. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    What are the actual errors?

    Often if there are clusters of a bunch of errors, the problem is a mismatched parenthesis or bracket, so check for those. (It's possible I had one in the code I typed, I didn't test it before posting it)
     
    BobRos likes this.
  16. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    It's 64 errors, I doubt you want to go through all of them. And I'm pretty sure it's not a bracket or a parenthesis, I've triple checked it. I think I might know what's wrong, going by the error statements. I'll try my best to explain it, I don't know if I'm right but this seems to be what the 64 errors are sayings. And I'm not trying to insult your coding or anything, just saying what it looks like is wrong. Anyways...

    Most of the errors are "does not exist in current context errors". For example, "x", "transform", or "position" "does not exist in the current context". I think the replacing "mainChar" with "PlayerMovement.playerInstance" might have messed something up. If I'm understanding this whole thing right, playerInstance doesn't have a transform, a position, or an x value so that's why it's not working. Again, not really sure, and that's only part of the problem.
    [edit]: I also thing there's something wrong with this line, in the first if statement.
    Code (CSharp):
    1. PlayerMovement.playerInstance.interactingWithDoor = this;
    this line doesn't work, but in the line a few lines down, in the second if statement it's the same except instead of "this" it says "null"
    Code (CSharp):
    1. PlayerMovement.playerInstance.interactingWithDoor = null;
    I don't know what the problem is exactly, but "this" isn't in Visual Studio's suggested words thing.


    So yeah, I don't really know what the problem is, there's just too many errors. So I'm just gonna start from scratch. If it's not too much trouble for you to just try writing the whole code, since I probably just made an error when I was putting your coding ideas into the script. Or, if you just don't feel like doing anything, that's fine. You've already helped a ton, so if you don't want to do any more that's perfectly fine. But if you do have an idea of why it could be getting errors, I'd like to know.
     
  17. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    OK, so the problem is probably that you've used the wrong name for the player script. "PlayerMovement" should be whatever the name of the player's class in the script is (which should be the same as the name of the .cs file for the script).

    If that is what the name is (to your understanding), select the player object in the editor, take a screenshot, and post it here - that should help.
     
    BobRos likes this.
  18. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Ok here's a screen shot of the hierarchy, and the main character in the inspector.

    The code that's going wrong is in another gameObject called "door" (you can see it in the hierarchy) I can take a screenshot of that in the inspector if you want, but that's not the player script you were talking about. The player script is PlayerMovement.
     

    Attached Files:

  19. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    'this' is a keyword that refers to the specific object on which the code is being run. It's usually implicit - for example, "this.transform.position" is the same as "transform.position".

    OK, can you paste in the relevant code from PlayerMovement.cs?
     
    BobRos likes this.
  20. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.  
    6.     public float speed = 200f;//walk speed
    7.     public float sprint = 300f;//sprint speed
    8.     public DoorScript interactingWithDoor = null;
    9.  
    10.     public static PlayerMovement playerInstance;
    11.     void Awake()
    12.     {
    13.         playerInstance = this;
    14.     }
    There's more to it, but the rest is just the script that moves the sprite when you press the movement keys. I didn't change that part, and it's not really relevant.
    Here's the rest of the relevant code, now that you know pretty much know everything that's going on.

    CursorLock:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CursorLock : MonoBehaviour
    5. {
    6.  
    7.     public static bool lockOutOf;
    8.  
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.     void Update()
    14.     {
    15.  
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         Cursor.visible = false;
    18.  
    19.         if (PlayerMovement.playerInstance.interactingWithDoor != null)
    20.         {
    21.             Cursor.lockState = CursorLockMode.None;
    22.         }
    23.         if (PlayerMovement.playerInstance.interactingWithDoor != this)
    24.         {
    25.             Cursor.lockState = CursorLockMode.Locked;
    26.         }
    27.  
    28.     }
    29. }
    DoorScript(errors underlined):

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class DoorScript : MonoBehaviour {
    7.  
    8.   public static bool lockOutOf;
    9.  
    10.  
    11.   // Use this for initialization
    12.   void Start () {
    13.  
    14.   }
    15.  
    16.   // Update is called once per frame
    17.   void Update()
    18.   {
    19.   public float minInteractionPositionX = -771.6f;
    20.   public float maxInteractionPositionX = -153.6f;
    21.  
    22.   float mainChar;
    23.   mainChar = GameObject.Find("MainChar").transform.position.x;
    24.  
    25.   if (PlayerMovement.playerInstance.transform.position.x >= minInteractionPositionX && PlayerMovement.playerInstance.transform.position.x <= maxInteractionPositionX && Input.GetKey(KeyCode.E))
    26.   {
    27.   PlayerMovement.playerInstance.interactingWithDoor = this;
    28.   }
    29.   if (PlayerMovement.playerInstance.interactingWithDoor == this && (mainChar.transform.position.x<minInteractionPositionX || PlayerMovement.playerInstance.transform.position.x> maxInteractionPositionX))
    30.   {
    31.   PlayerMovement.playerInstance.interactingWithDoor = null;
    32.   }
    33.   }
    34.  
    35.  
    36.    
    37. }
    Again, I totally understand if you don't feel like doing all this. But if you do, It would really help.
     
  21. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    OK..... hm. This is odd. That should be compiling correctly.

    What's the exact, full text of the first compile error in the list?
     
    BobRos likes this.
  22. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Oh, one issue (may or may not be related to the main ones): the "public float" lines should be outside the Update function, in the main body of the class.
     
    BobRos likes this.
  23. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    The first compile error is just "} expected" in line 17.
    The second is "{ expected" in 28.
    After that, it gets pretty complex
     
  24. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    WTF THAT JUST FIXED ALL THE ERRORS!
    well there's still one, it says
    'float' does not contain a definition for 'transform' and no extension method 'transform' accepting a first argument of type 'float' could be found (are you missing a using directive or an assembly reference?) in line 28.
    But yeah, other then that it's compiling so again, THANK YOU!
     
  25. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    OK, that first error I believe is caused by the problem in my most recent comment.

    Fix that; if there are still errors, then paste in the full text of the first error again. (You can copy and paste them out of the Console window, btw)
     
    BobRos likes this.
  26. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Ok I just got it to compile. I changed the "mainChar" in line 28 to "PlayerMovement.playerInstance"
    Now I'm gonna run it, see if it works.
     
  27. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Fun fact about programming... a script with 200 errors is often easier to fix than a script that compiles but fails silently ;)
     
    BobRos and Jamster like this.
  28. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Okay it didn't crash the game or anything, and it's compiling, so that's progress.
    The only thing is, now it just doesn't do anything. I go near the door, I click "e" and nothing happens. The cursor stays locked. Any ideas how to get that to work?
     
  29. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Sometimes, the mouse does appear for like, 1 frame when I press "e".
    Could this mean that maybe it's working, but something is making it lock again the second that it unlocks?
     
  30. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Have the PlayerMovement inspector open when you play the game. There should be a "Interacting With Door" slot on it (right by the "speed" and "sprint" properties); watch that space as you walk up and press E. Does it get filled with a reference to the door object, or does it stay empty/null?
     
    BobRos likes this.
  31. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Seems likely that the second 'if' statement is malformed, then.

    You could confirm this by putting a few Debug.Log statements within the two if statements - one outputs "Setting", one outputs "Unsetting". Then you watch the console and see which of those lines gets outputted. If they both appear almost simultaneously, your hypothesis is correct.
     
    BobRos likes this.
  32. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    When I'm outside of the area, it's filled with "None (Door Script)"
    When I'm inside and I press "E", it says "FullscreenGUI (Door Script)"
    FullscreenGUI is the GUI I was trying to make appear earlier. Does that help?
     
  33. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Also, I don't really know how to do that Debug.Log statement thing you were talking about (new to coding). So if it's really necessary I could probably google it, or you could explain it to me if its not too complicated. If it's not essential though, I just wont do it.
     
  34. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Never mind actually, I did the debug.log thing. The first if statement (setting) came first but then the second one (unsetting) came right after it. And they did appear almost simultaneously.
     
  35. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48

    So since I did the debug.log, and that was the problem, how do I fix my if statements being malformed?

    Sorry if I just bombarded you with like three questions at once
     
  36. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Debug.Log is one of the most fundamental ways of debugging code, so although your other comment has made it unnecessary for this task, you should learn how to use it.
    Code (csharp):
    1. Debug.Log("Some message");
    2. Debug.Log("Some message", someObject);
    3. Debug.Log("At time "+Time.time+", the value is "+someValue);
    The first one is useful for just letting you know that code has gotten to a particular point - which is all we were going for in this case.

    The second one is useful for finding objects. If you have a script on 200 objects and only one of them is giving you trouble, you can pass it as the second parameter. Now, when you click on that message, the object that was passed will be highlighted in the editor (like, bright yellow bouncy highlight) so you can track it down.

    The third is an example of getting more detailed information in a log.
     
    BobRos likes this.
  37. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    I think this would be a good chance to use a Debug.Log and spit out the relevant numbers - minInteractionPositionX, PlayerMovement.playerInstance.transform.position.x, etc. Look at the values it puts out. Take those values, and imagine the 'if' statement as if it had those values in place of the variables that were holding them - you should be able to find which part of it is giving you an unexpected answer.
     
    BobRos likes this.
  38. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Thanks for the explanation. I think I'm getting closer. I changed the second if statement to an "else" statement, and now it works. The mouse appears, and when I move out of range it dissapears. The only thing is, for some reason, the mouse is still kind of locked. It's weird, l can move it around a little bit, but it's very choppy, and it centers itself whenever I move it from the center.
     
  39. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    OK, I think the problem is now in your cursor locking script. Check out line 16 - you set it to locked every frame, before you set it to something else. I suspect that that's causing your weird cursor behavior.
     
    BobRos likes this.
  40. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Could you just tell me what to change in line 16? I think you wrote that code, and I don't really know how to edit it without screwing it up. Here it is:
    Code (CSharp):
    1.         Cursor.lockState = CursorLockMode.Confined;
    I don't really understand what's going on/ what the problem is, so it'd be easier if you just rewrote it so that it works. Or if you think you can explain it pretty easily, that'd be even better, but I don't mind if you just go and fix it. Full script, if you need it:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CursorLock : MonoBehaviour
    5. {
    6.  
    7.     public static bool lockOutOf;
    8.  
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.     void Update()
    14.     {
    15.  
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         Cursor.visible = false;
    18.  
    19.         if (PlayerMovement.playerInstance.interactingWithDoor != null)
    20.         {
    21.             Cursor.lockState = CursorLockMode.None;
    22.             Debug.Log("setting");
    23.             Cursor.visible = true;
    24.         }
    25.         //if (PlayerMovement.playerInstance.interactingWithDoor != this)
    26.         else
    27.         {
    28.             Cursor.lockState = CursorLockMode.Locked;
    29.             Debug.Log("johncena");
    30.             Cursor.visible = false;
    31.         }
    32.  
    33.     }
    34. }
    35.  
     
  41. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Line 16 is redundant (also line 17) - no matter which branch of the 'if/else' it goes into, both of those things are going to be overwritten with something else. Just remove them entirely.
     
    BobRos likes this.
  42. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    yoooooooouuuuu sonofabitch
     
    BobRos likes this.
  43. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    actually just miscounted lines I guess, so I think I get it. I'll try that once I restart unity. Just as an idea, I changed the if statements to while statements, and my whole computer froze, had to ctrl alt delete. So give me a minute, ill try it and tell you what happens.
     
  44. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Although I did try commenting those lines out before unity crashed, and if I remembed correctly it didn't work. So any other ideas?
     
  45. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Yeah, don't use while statements unless something inside the statement is going to make the condition become false at some point. :)
     
    BobRos likes this.
  46. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    You may be on your own at this point, it's been years since I've used cursor locking, and at this point I'd probably have to experiment just like you would.
     
    BobRos likes this.
  47. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    I'll need a few minutes to fix everything, since I hadn't saved in a while cause I'm an idiot. So sorry for the wait, should only take a minute or two.
     
  48. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    Alright, well thanks a ton for all your help. If there's any way I can +rep or commend you or something, I'd be happy to do it. Again, thanks so much, I really really appreciate it.
     
  49. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    [UPDATE]
    just though i'd let you know, IT WORKS! so thanks soooooo much, I will now proceed to like all your posts, since I can't figure out how to +rep on this site.