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 /
  • Help Room /
avatar image
0
Question by DJWolfang25 · Nov 10, 2015 at 04:39 PM · c#scorerespawn

C# Respawn and Score Problem

So I'm making a pong game and I'm trying to make the ball re-spawn and the score go up by 1 point every time it passes the paddle. Currently when the ball passes the paddle, the score goes up by one and keeps going up indefinitely and ball doesn't re-spawn.

Here is my code:

using UnityEngine; using System.Collections;

public class Score : MonoBehaviour {

 public TextMesh currSco;
 public GameObject ballPref;
 public Transform paddleObj;

 GameObject ball;
 int score;

 void Update () 
 {
     ball = GameObject.FindGameObjectWithTag("Ball");
     currSco.text = "" + score;
 }
 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Ball")
     {
         score += 1;
         Destroy(ball);
         (Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 2, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject).transform.parent = paddleObj;

     }
 }
 

}

I also have a "Ball" gameobject and i put my paddle under paddle obj in the script boxes in unity. Can someone please help?

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
0

Answer by Soraphis · Nov 10, 2015 at 05:23 PM

Why dont you just reset the ball position?

other.transform.position = new Vector3(......);

so you don't have to a) set the ball variable every frame. b) you don't need a reference to the ball at all. and you should do other.CompareTag("Ball")

and you shouldn't update the text each frame like this ... it would be enough to change the text after increasing the score

for the "why doesn't it work" I've got no clue. i could imagine it has something to do with your hierarchy. i dont get why you put your ball as a child of your paddle object...

Comment
Add comment · Show 2 · 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 DJWolfang25 · Nov 12, 2015 at 04:57 PM 0
Share

What would I need to replace for the "other.transform.position = new Vector3()" code?. Also, the reason for making the ball a child of the paddle is so the ball moves with the paddle before the ball is shot out. Also isn't line 11 supposed to increase the text by the score? I should also say that it only happens when i put the paddle into the paddle obj slot under the score script.

avatar image DJWolfang25 DJWolfang25 · Nov 12, 2015 at 05:08 PM 0
Share

This was the tutorial that I was using: https://www.youtube.com/watch?v=0YLDz$$anonymous$$kXl$$anonymous$$E

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity/C# Respawn and score Problem 1 Answer

Score value in GameOver screen is 0 instead of final value from the Game Level scene(used PlayerPrefs), how do I display the final value from Game Level scene in the GameOver scene? 1 Answer

how to disable dontdestroyonload on a specific scene 2 Answers

Best Score Counter 0 Answers

Game highscore file writing 0 Answers


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