- Home /
Getter function not returning correct value from another script (C# scripting)
Hey there everyone I'm a new user who is using Unity for a college class on video game design/programming. I'm trying to use a public getter function I wrote, shown here
public int getHP()
{
return HP;
}
getHP() and HP are the getter and a private int (respectively) in a script I wrote called playerControl
. However my controller object is not returning the proper value of the player's current HP. This is the code I'm using to get the player's HP:
public class healthControl : MonoBehaviour
{
**// This is the object that the health controller finds the playerControl script**
**// I assigned this field the player object from my prefabs in the inspector**
public GameObject player;
[SerializeField]
**//Array that holds the animation frames of the health sprite**
internal Sprite[] mySpr = new Sprite[25];
private SpriteRenderer render;
**// Used for the mySpr index. This is what frame the sprite is on**
private int frame;
void Start()
{
**//playerControl is the script that holds all the player's logic, including their getter functions**
frame = player.GetComponent<playerControl>().getHP();
render = GetComponent<SpriteRenderer>();
render.sprite = mySpr[frame];
}
void Update()
{
** //DEBUG - print the value of "frame" (it always prints 0 ?????)**
print(frame);
**//Get the player's HP and make the sprite draw their health**
frame = player.GetComponent<playerControl>().getHP();
render.sprite = mySpr[frame];
}
}
the player's private HP
integer is created at the top of playerControl
but is not assigned a value until the Start
function, at which point HP
and maxHP
(another private integer in playerControl
) are both assigned the value of 24. If I call getHP()
from inside my player object, then the correct value of the player's HP is returned, but if I call getHP()
from the health controller object then it always returns 0. Even while running, it constantly returns 0, as though the player's HP variable is never assigned a value or gets changed at all.
Why isn't getHP()
returning the player character's health and instead always returning 0?
Answer by termway · Sep 09, 2019 at 11:09 PM
First I would suggest you to cache the playerControl component in a variable. Then I would add a breakpoint with visual studio to see the value of your playerControl component to see what's going on (https://docs.unity3d.com/Manual/ManagedCodeDebugging.html). I also suggest you to check the reference of your playerControl. It may be not the one you want to use.
Your answer
Follow this Question
Related Questions
How can I take a variable from another script and then apply it to a newly insantiated prefab? 2 Answers
Issue calling a variable from another script - C# 1 Answer
How to change a variable on the script of an instance that is one of many 0 Answers
How to make Vector3s communicate with Floats? 2 Answers
An instance of type 'Script' is required to access non static member 'Variable' 1 Answer