• 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
0
Question by YetiX · Apr 03, 2021 at 02:12 PM · unity 5instantiatescenenewbie

Restart the scene without changing the scoreboard

I was making a pong-like game and I have reached the point where I have to reset the ball to its original position every time a player scores in the opponent's goal. I have seen that you can use the SceneManager to reload the initial scene although this also resets the score to its original state.

I have also thought, instead of reloading the main scene, to simply reload a ball, using the Instantiate function. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class MovPelota : MonoBehaviour
 {
     // Start is called before the first frame update
     public float ballspeed,movx,movy;
     float maximoX, maximoY;
     float minimoX, minimoY;
     float objectWidth, objectHeight;
     public Vector2 screenBounds;
     public Camera MainCamera;
     public event System.Action PuntoEnemigo;
     public event System.Action PuntoJugador;
     public Vector3 originalpos = new Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z);//Original Position of the Ball
     
     void Start()
     {
        
         screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, MainCamera.transform.position.z));
         
         ballspeed = 5f;
         objectWidth = transform.GetComponent<Renderer>().bounds.extents.x; 
         objectHeight = transform.GetComponent<Renderer>().bounds.extents.y; 
 
         //Maximo y minimo de las pantallas
         maximoX = screenBounds.x * -1 + objectWidth;
         minimoX = screenBounds.x - objectWidth;
         maximoY = screenBounds.y * -1 + objectHeight;
         minimoY = screenBounds.y - objectHeight;
 
         //Movimiento de la pelota.
         movx = -.5f;
         movy = -.5f;
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector3 viewPos = transform.position;
         
         Puntuaciones(viewPos);
         if (viewPos.y <= maximoY)
         {
             viewPos.y = ((float)(maximoY - 0.05f));
             movy *= -1;
         }
         if (viewPos.y >= minimoY)
         {
             viewPos.y = ((float)(minimoY + 0.05f));
             movy *= -1;
         }
             transform.Translate(movx * Time.deltaTime * ballspeed, movy * Time.deltaTime * ballspeed, 0f);
         
 
     }
     void OnTriggerEnter2D(Collider2D trigger)
     {
         Vector3 viewPos = transform.position;
         if (trigger.tag == "jugador")
         {
             viewPos.x = ((float)(maximoX - 0.05f));
             movx *= -1;
             
         }
 
     }
     void Puntuaciones(Vector3 PosicionNueva)//Function for points
     {
         if (PosicionNueva.x <= maximoX )
         { 
             if(PuntoEnemigo != null)
             {
                 PuntoEnemigo();
                 
                
              }
              Instantiate(pelota,originalpos);//pelota is a game object in the scene
 
              
         }
         if (PosicionNueva.x >= minimoX)
         {
             if(PuntoJugador != null)
             {
                 PuntoJugador();
                
             }
             Instantiate(pelota,originalpos);
         }
     }
 }

I'm new to unity and c#, so I don't understand when it says that "ball" doesn't exist in this context, I don't know if it's because it has to be declared as a game object even though it exists in the main scene.

alt text

I wanted to know how to do what I am trying to do well or if there is another way to do it more efficiently. (17,78)Los errores que me aparecen son : Error CS0236: A field initializer cannot reference the non-static field, method, or property 'Component.gameObject' (17,110): error CS0236: A field initializer cannot reference the non-static field, method, or property 'Component.gameObject' (80,26): error CS0103: The name 'pelota' does not exist in the current context (90,25): error CS0103: The name 'pelota' does not exist in the current context

unityball.png (6.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

238 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add different audios on different scenes 0 Answers

How to save not only the position of player but the whole state of my scene ? 0 Answers

How to Reference Player when Instantiating Prefab 1 Answer

Creating a primitive during scene preprocessing? 0 Answers

[HELP WANTED] Blender FBX to Unity Animation Issue 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