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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Minor issue with timing of UI popup box(Javascript)

Discussion in 'Scripting' started by Wolfsshadow, Sep 11, 2013.

  1. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    Hello all!

    Just for the life of me haven't been able to figure out what's wrong with my code (considering I just copied then modified from another object that does the same thing but work correctly). It uses two codes. One to detect when the player is within range, and the other to put up the popup box and then load the level. The level load works correctly, but the box is always there instead of like the other boxes how it comes and goes based on my range to the object. I wasn't sure if maybe I put it on another object or not, so I deleted the script, it went away, and then I re-did the script only to find it doing it again. Here are my scripts:

    Display GUI Script_FlyingGame.js:


    var target : Transform;

    function Update (){
    var other = gameObject.GetComponent("GUI Style Script_Flying Game");

    if(Vector3.Distance(target.position, transform.position) > 5) {
    other.enabled = false;
    }
    else if(Vector3.Distance(target.position, transform.position) < 5) {
    other.enabled = true;

    }
    }

    GUI Style Script_FlyingGame.js:


    var target : Transform;

    function Update (){
    var other = gameObject.GetComponent("GUI Style Script_Flying Game");
    }


    function OnGUI() {
    //Make a background box
    GUI.Box (Rect (400,400,300,90), "Do you want to play the Flying Game?");

    //Make the first button. If it is pressed, Application.Loadlevel("Flying Game") will be executed
    if(GUI.Button (Rect(450,450,80,20), "Yes")) {
    Loading_Screen.LoadingScreenOn = true;
    Application.LoadLevel("Flying Game 2");
    }
    if(GUI.Button (Rect(600,450,80,20), "No")){
    "GUI Style Script_Flying Game".enabled = false;


    }
    }



    I really appreciate the help. I try to fix things when I can, but it's been about a week of trying to find this problem to no avail.


    **Edited to include script names for clarity**
     
    Last edited: Sep 12, 2013
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    I am inexperienced in the string version og GetComponent,but can this really be a valid component name? "UI Style Script_Flying Game"

    Besides that, your distance script will only deactivate/activate the "
    UI Style Script_Flying Game" component, not sure if that is your script with the box as you didnt mentioned this clearly.
     
  3. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    I added the script names to their respective scripts to make it clearer(sorry about that). I'm really confused because this exact script minus the "Flying Game" part is exactly what I use for my other "portals".
     
  4. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    ***UPDATE***
    I got it to go away by correcting the name to FlyingGame without a space, but now when approaching it won't turn on.....
     
  5. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    ***UPDATE***
    Fixed, just had some of them mixed up as to what they were attached to on that last trial. Thanks for taking a look at it for me.