Search Unity

Script wont work

Discussion in 'Scripting' started by Wicked_Games, Jul 6, 2015.

  1. Wicked_Games

    Wicked_Games

    Joined:
    Apr 24, 2015
    Posts:
    5
    My script is for respawning the player and show a gui to click enter to respawns ,but when the player dies he respawns without needing to press enter and there is no gui showing.

    if(playerisDead){
    Respawn.SetActive(true);
    if (!audioPlayed)
    {
    GetComponent<AudioSource>().clip = MarioSound ;
    GetComponent<AudioSource>().pitch = Random.Range(0.85f, 1.1f);
    GetComponent<AudioSource>().Play();
    audioPlayed = true;
    }
    if(Input.GetKeyDown(KeyCode.Return));
    {
    RespawnPlayer();
    }

    }
    }
    void OnGUI()
    {

    }
    void RespawnPlayer()
    {
    Respawn.SetActive(false);
    Vector3 newPos = new Vector3(Random.Range(Xmin,Xmax),Random.Range (Ymin,Ymax),Random.Range(Zmin,Zmax));
    Object PlayerRagdoll = Instantiate(PlayerDead,transform.position,Quaternion.identity)as GameObject;
    transform.position = newPos;
    playerisDead = false;

    Untitled-1.jpg
     
    Last edited: Jul 6, 2015
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Copy/paste your code in here with code tags. It's hard to tell what the problem is from a screenshot of some of the code.
     
  3. Wicked_Games

    Wicked_Games

    Joined:
    Apr 24, 2015
    Posts:
    5
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    there is a sticky on code tags at the top of the scripting forum, they really help make code readable on the forums
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    looks like you have included the old legacy OnGUI function... but haven't told it to do anything, so it's not going to show anything. You're probably better off looking at the tutorials on the new UI system in unity from 4.6+

    http://unity3d.com/learn/tutorials/modules/beginner/ui


    also what is "Respawn" on line 18 and 25 referring to? neither the screen shot or the code you pasted includes the declaration of that variable. Does that have a script attached to it?
     
  6. Wicked_Games

    Wicked_Games

    Joined:
    Apr 24, 2015
    Posts:
    5
    i am not using the OnGUI function that was something i just forgot to delete. The gui is shown with a empty game object with two gui textures attached being turned off and on.
     
  7. Wicked_Games

    Wicked_Games

    Joined:
    Apr 24, 2015
    Posts:
    5
    It is a GameObject, its declared but not in the image.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    so "Respawn" is a gameobject in the scene that you are using as a UI?
     
  9. Wicked_Games

    Wicked_Games

    Joined:
    Apr 24, 2015
    Posts:
    5
    yes. It acting as a group holder for two gui textures