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 /
avatar image
1
Question by Cryno1000 · Sep 16, 2017 at 12:40 AM · beginnergameover

I need help on a win/lose script.

I am making a pong clone, and if the ball goes off one side of the screen, it will say "You lose" but if it goes off the other, it will say "You Win"

Please write it for me, since I don't know how to script that much and this has been frustrating me for a long time.

Other things you may need to know:

I'm writing in C#

It's 3d

Position for the You Lose side: -13.89, 0, 0

Position for the You Win Side: 13.89, 0, 0

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by thealexguy1 · Sep 16, 2017 at 03:26 AM

Firstly, you will want to create a UI text to show the message. Right click in the hierachy and go UI -> text. Attach this script to the UI text you just made.

 using UnityEngine.UI;   //This is important as it allows you to edit UI elements
 public Transform theBall;  //This is the position of the ball, once the code is written and attached, drag the ball gameobject onto this in the Inspector
 
 void Start(){
       GetComponent<Text>().enabled = false;  //Hide the text when the game starts
 }
 void Update(){
       if (theBall.position.x >= 13.89){   //Check if the ball goes off to the right
              GetComponent<Text>().enabled = true;   //Show the text
              GetComponent<Text>().text = "You win";    //Set the text to "You win"
              Time.timeScale = 0;   //Pause the game
       }
      else if (theBall.position.x <= -13.89){    //Check if the ball goes off to the left
              GetComponent<Text>().enabled = true;   //Show the text
              GetComponent<Text>().text = "You lose";    //Set the text to "You lose"
              Time.timeScale = 0;   //Pause the game
       }
 }


I have not tested this but it should work... :/

Comment
Add comment · Show 3 · 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 FayyadSufyan · Sep 16, 2017 at 04:37 AM 0
Share

Be aware that GetComponent is a bit slow, cache a reference to it in Start() or in inspector by setting it public or private with the attribute [SerializeField]

avatar image Cryno1000 · Sep 16, 2017 at 01:20 PM 0
Share

Thanks, but I keep getting an error saying "Assets/YouWinLoseScript.cs(2/7) error CS1525: Unexpected symbol 'Transform', expecting 'class', 'delegate', 'enum', 'interface', 'partial', or 'struct'"

I hardly know anything about C# so if you could help, I would be in your debt. Thanks

avatar image thealexguy1 · Sep 16, 2017 at 08:33 PM 0
Share

Hi, sorry for not clarifying, but when you make a new script, the line 'public class YouWinLoseScript : $$anonymous$$onobehaviour { }, make sure the 'using UnityEngine.UI' is above that, just underneath the 'using Unity.SystemCollections', and everything else from the 'public Transform theBall' down is all inside those main {}s, below it. If you don't understand let me know.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How angry bird's "GAME OVER" system work? 2 Answers

What to start with when using Unity 3? 6 Answers

Camera for 3D game Help 1 Answer

Animations not usable 2 Answers

Creating Game Objects 1 Answer


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