Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
How to access methods after Instantiating a player prefab in GameManager?

I am fairly new to unity and am trying to make a game based on mapbox location based game. Right now I have an welcome scene where they choose a name and a character (male or female), which works perfectly fine and they chosen character shows on the map.

Now, I have three scripts: GameManager, Player & BonusXP. In the GameManager I am instantiating the chosen player and place them on the map.

In the player script I have certain variables such as xp and level and methods like AddXP().

In the bonusXP script which I attached to a random object, when that one is clicked there needs to be added a certain amount of XP to the player. In this example I used 10. Now I had it working fine until I added the choose a character functionality. Before I would drag the player into the GameManager serialised field and stuff worked. Now that that one is gone. It stopped working. How do I add XP to a instantiated Player chosen by the user?

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;
//      }
//  }
  
}
`

The BonusXP Script:

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);
    }
}

The necessary parts of player script:

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 Respostas
Responde à pergunta

0

First, I would suggest to create your players as Prefabs and directly add your Player script to them. This way you won't be calling:

 clone.AddComponent<Player>();

in Awake method of your GameManager.

The problem with your current implementation is that you are not storing anywhere the Player object you instantiate. Try something like this:

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>();
}

This way you are setting your CurrentPlayer to the selected one and using GameManager.Instance.CurrentPlayer.AddXp(); should work.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda