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

170
Views
¿Cómo acceder a los métodos después de crear una instancia prefabricada de un jugador en GameManager?

Soy bastante nuevo en Unity y estoy tratando de hacer un juego basado en un juego basado en la ubicación de Mapbox. En este momento tengo una escena de bienvenida en la que eligen un nombre y un personaje (masculino o femenino), que funciona perfectamente bien y eligen los personajes que se muestran en el mapa.

Ahora tengo tres scripts: GameManager, Player y BonusXP. En el GameManager estoy creando una instancia del jugador elegido y lo coloco en el mapa.

En el script del jugador tengo ciertas variables como xp y nivel y métodos como AddXP().

En el script bonusXP que adjunté a un objeto aleatorio, cuando se hace clic en él, se debe agregar una cierta cantidad de XP al jugador. En este ejemplo, usé 10. Ahora lo tenía funcionando bien hasta que agregué la funcionalidad de elegir un personaje. Antes, arrastraba al jugador al campo serializado de GameManager y todo funcionaba. Ahora que ese se ha ido. Dejó de funcionar. ¿Cómo agrego XP a un jugador instanciado elegido por el usuario?

 The Gamemanager script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManager : Singleton<GameManager> { public GameObject[] players; public Transform spawnPoint; private void Awake() { int selectedC = PlayerPrefs.GetInt("selectedcharacter"); GameObject prefab = players[selectedC]; GameObject clone = Instantiate(prefab, spawnPoint.position, Quaternion.identity); clone.AddComponent<Player>(); //attaches player script to the clone } // public Player CurrentPlayer { // get // { // if (currentPlayer == null) { // } // return currentPlayer; // } // } } `

El guión BonusXP:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class XPBonus : MonoBehaviour { [SerializeField] private int bonus = 10; private void OnMouseDown() { GameManager.Instance.CurrentPlayer.AddXp(bonus); Destroy(gameObject); } }

Las partes necesarias del guión del jugador:

 public class Player : MonoBehaviour { [SerializeField] private int xp = 0; [SerializeField] private int requiredXp = 100; [SerializeField] private int levelBase = 100; public void AddXp(int xp) { this.xp += Mathf.Max(0, xp); } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Primero, sugeriría crear sus reproductores como Prefabs y agregarles directamente su secuencia de comandos Player . De esta manera no estarás llamando:

 clone.AddComponent<Player>();

en el método Awake de su GameManager .

El problema con su implementación actual es que no está almacenando en ningún lugar el objeto Player que instancia. Prueba algo como esto:

 public Player CurrentPlayer {get; private set;} private void Awake() { var selectedCharacter = PlayerPrefs.GetInt("selectedcharacter"); var playerObj = Instantiate(players[selectedCharacter], spawnPoint.position, Quaternion.identity); CurrentPlayer = playerObj.GetComponent<Player>(); }

De esta manera, está configurando su CurrentPlayer en el seleccionado y usando GameManager.Instance.CurrentPlayer.AddXp(); Deberia trabajar.

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!