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

165
Views
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 answers
Answer question

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 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!