Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
La variable Unity C# no funciona dentro de OnCollisionEnter

Estoy trabajando en un juego que consiste básicamente en hacer rodar una pelota y cuando golpeas el objeto Gameover, es el fin del juego, cuando golpeas el objeto objetivo, es el objetivo (y tal vez te lleven a la siguiente etapa o lo que sea), y aquí quiero un algoritmo. se ignorará cuando golpees el objeto del final del juego después de que golpees el objeto objetivo, por lo que una vez que alcances el objeto objetivo, ganarás el escenario.

Aquí, el código C#, configuré la variable goaled e intenté cambiar la variable a "verdadero" cuando golpeas el objeto objetivo, y cuando solo si goaled == false , cuando aún no hayas apuntado, estarás perdido. Pero el código no funciona, como resultado esperado, no debería registrar "gameover" después de marcar, pero en realidad lo hará.

Entonces, ¿cómo puedo modificar el código para que funcione como se esperaba? Gracias.

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class tmp03 : MonoBehaviour { bool goaled = false; void OnCollisionEnter(Collision otherObj) { // bool goaled = false; if (gameObject.name == "goal" && otherObj.gameObject.name == "ball") { Debug.Log("goal"); Debug.Log(goaled); goaled = true; Debug.Log(goaled); } if (gameObject.name == "gameover" && otherObj.gameObject.name == "ball" && goaled == false) { Debug.Log("gameover"); Debug.Log(goaled); } } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Por lo que entiendo de su código, específicamente de las líneas gameObject.name == "goal" && otherObj.gameObject.name == "ball" & gameObject.name == "gameover" && otherObj.gameObject.name == "ball" && goaled == false , me parece que está agregando este script tanto a los objetos de juego Goal como GameOver .

Estas son DOS instancias de script DIFERENTES. No compartirán la propiedad goaled entre ellos. Si desea rastrear solo UNA propiedad goaled , debe agregar el script solo una vez (en este caso, al reproductor) e invertir las condiciones if. Haz que la pelota sea el gameObject principal, y el gameover object y el objetivo como el otherObj

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!