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

need a script

Discussion in 'Scripting' started by ahmedalheday, May 3, 2014.

  1. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    i need a scpit that can start the game and quit game
    i have tried lot of them but nothing worked
     
  2. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    The scripts i'll give you are in Javascript, so you need to paste them in Javascript script, notC#.

    First thing you need to do is to set up your build : go to File/Build Settings and add the scene you want in your build (Your menu should be 0 and your first level should be 1.)If you don't do that, scripts wont work.

    Next, create a new GUITexture in your Menu Scene. Put there any texture you want, this will be the LoadGame button.

    Create a Javascript file. Open it and paste this :


    Code (csharp):
    1. #pragma strict
    2.  
    3. var couleurBouton : Color;
    4.  
    5.  
    6. function OnMouseDown(){
    7.    
    8.     //This will load the level (I've put 1 assuming your level is 1 in your build, but you can (must) change that accordingly to how your build is set up.
    9.     Application.LoadLevel(1);
    10.  
    11. }
    12.  
    13. function OnMouseEnter(){
    14.  
    15.     //This will change the color of your button (here to a green color)
    16.     couleurBouton = Color(0.5, 0.8, 0.5);
    17.     guiTexture.color = couleurBouton ;
    18. }
    19.  
    20. function OnMouseExit(){
    21.  
    22.     //This will reset the color of your button
    23.     couleurBouton = Color(0.5, 0.5, 0.5);
    24.     guiTexture.color = couleurBouton ;
    25.    
    26. }
    Save the script.Then you have to put this script on your Load game button.

    Next, create a new GUITexture and assign to it your Quit texture.
    Create a new Javascript script. Open it and paste this code:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var couleurBouton : Color;
    4.  
    5.  
    6. function OnMouseDown(){
    7.    
    8.     //This will quit the game.
    9.     Application.Quit();
    10.  
    11. }
    12.  
    13. function OnMouseEnter(){
    14.  
    15.     //This will change the color of your button (here to a green color)
    16.     couleurBouton = Color(0.5, 0.8, 0.5);
    17.     guiTexture.color = couleurBouton ;
    18. }
    19.  
    20. function OnMouseExit(){
    21.  
    22.     //This will reset the color of your button
    23.     couleurBouton = Color(0.5, 0.5, 0.5);
    24.     guiTexture.color = couleurBouton ;
    25.    
    26. }
    Save it. Add this new script to your Quit Button.

    That's it you now have a start button and a quit button. Here i gave you all the steps, if you can't make it work, redo some tutorials showing the very basics of unity and coding.
     
  3. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    ok
    i already have the text i have made them but i only have the start and Quit
    but u side loedgame ?
    how do i do that ?
     
  4. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Start will just do fine, but what it really does is to load a level from your build. Again, if you don't set up your build, this won't work. But the quit button will work even if you don't set up your build. But the quit button won't work if you play your game in the editor, you must build your game and play the builded game to have the quit button. In editor, it's ignored because you just have to hit the start stop button of the editor.
     
    Last edited: May 3, 2014
  5. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    works beautifuly
    thank u sooooo much
    i was trying to fix this the last 4 days
    all day up

    the quit does not work coz i am in the game

    sir if i am right the first script is for the start and the other script is for quit right ?
     
  6. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Indeed, first script is to start your game, the second one (wich will only work in builded game, not in Unity) is the quit button.
     
  7. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    it showes me this

    MissingComponentException: There is no 'GUITexture' attached to the "quit" game object, but a script is trying to access it.
    You probably need to add a GUITexture to the game object "quit". Or your script needs to check if the component is attached before using it.
    UnityEngine.GUITexture.set_color (Color value) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/Graphics.cs:3231)
    strt and quit.OnMouseExit () (at Assets/strt and quit.js:47)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

    but it works great
     
  8. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    The error is there because your buttons must be GUIText and not GUITextures. You have 2 solutions to get this work completly. First is just to remove the OnMouseEnter and OnMouseExit functions of the scripts : The button will still work but won't change color. The second one, wich you should do is to open a program like photoshop, create 2 files, type Start in one, Quit in the secon and save those files as PNG. Then you go back inUnity, go to your Menu scene, and create 2 GUITextures. Import those 2 image you just made. Drag the Start texture in the inspector of one of the GUITextures, drag the other to the other. poisition them and add the start script to the start GUI texture and the quit script to the Quit GUITexture. And you'll no longer have errors messages.
     
  9. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    i am a photoshop artist
    $Screenshot (87).png
    this is how every thing looks
    that error was a old one so dont worry

    but even if i make the game complet it well the error effect it ?
     
  10. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    The game should work even with that error, because it's a runtime error, not a compiling error. But's it's better to not have runtime errors.
     
  11. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    so i have to fix it ?
     
  12. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Yeah, you should. Just remove the OnMouseEnter and OnMouseExit functions (and the couleurBouton variable) from the scripts.
    This would be enough to make buttons work :

    Start button :

    Code (csharp):
    1. #pragma strict
    2.  
    3.  
    4.  
    5. function OnMouseDown(){
    6.  
    7.    
    8.  
    9.     //This will load the level (I've put 1 assuming your level is 1 in your build, but you can (must) change that accordingly to how your build is set up.
    10.  
    11.     Application.LoadLevel(1);
    12.  
    13.  
    14.  
    15. }

    Quit button :


    Code (csharp):
    1. #pragma strict
    2.  
    3.  
    4.  
    5. function OnMouseDown(){
    6.  
    7.    
    8.  
    9.     //This will quit the game.
    10.  
    11.     Application.Quit();
    12.  
    13.  
    14.  
    15. }
     
    Last edited: May 3, 2014
  13. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    so i add them to to the script
     
  14. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    No , don't add this to the scripts, replace it.
     
  15. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
  16. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    works great
     
  17. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
  18. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    thank u so much sir
    pls can i get your contact
    email facebook
    if u dont mind plss
     
  19. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
  20. ahmedalheday

    ahmedalheday

    Joined:
    Feb 24, 2014
    Posts:
    103
    sir can u help me now on a levels script
    for each levels
     
  21. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    I prefer to not give you my email or facebook, i keep those for IRL friends mostly. I can't give you scripts for your levels, because that may be a huge task, depending on what you actually want to happen in those level. ( and that i don't even know what kind of game you want to make) The way to go is try yourself and then ask here for specific questions that you may encounter during the process of making your game. But there is no such thing as "level script", level are composed, in most simple case of dozens of scripts, so try to do it yourself and ask specific questions like "I'm trying to do a script to do this but it don't works, here is the script, can you tell me if you find where is the error please". Post scripts that don't work as intended, else people won't be able to help you.