0

I'm working on a demo audio project in Unity 5 and coming across some issues. My goal is to assign the value of currentHealth (which is contained inside a separate "PlayerHealth" script) to a parameter in FMOD that modulates the music (makes it more intense) as the health of the player decreases. I am not a programmer by any means, but have been forced to get my hands dirty to implement the music in Unity 5.

HealthParam.setValue (PlayerHealth.currentHealth);

This line of code is making Unity unhappy. I've gathered it might have something to do with the difference between static and instance members, but again I'm not a programmer and really don't know how to delve into the weeds on this.

The errors are:

"Argument #1' cannot convertobject' expression to type `float'"

"The best overloaded method match for `FMOD.Studio.ParameterInstance.setValue(float)' has some invalid arguments"

"An object reference is required to access non-static member `PlayerHealth.currentHealth'"

Here is the Script:

using UnityEngine;
using System.Collections;

public class MusicManager : MonoBehaviour 
{


public int startingHealth = 100;                            
public int currentHealth;
public PlayerHealth playerHealth;
GameObject player; 

[FMODUnity.EventRef]
public string ScaryMusic = "event:/ZombunnyHorrorMusic";
FMOD.Studio.EventInstance MusicEv;
FMOD.Studio.ParameterInstance HealthParam;


void awake()
{
//      player = GameObject.FindGameObjectWithTag ("Player");
//      playerHealth = player.GetComponent <PlayerHealth> ();
}

void Start () 
{
    MusicEv = FMODUnity.RuntimeManager.CreateInstance (ScaryMusic);
    MusicEv.getParameter ("Health", out HealthParam);
    MusicEv.start ();
}

void update()
{
//      currentHealth = Mathf.Abs (PlayerHealth.currentHealth);
    HealthParam.setValue (PlayerHealth.currentHealth);
}

void OnDestroy ()
{
    MusicEv.stop (FMOD.Studio.STOP_MODE.IMMEDIATE);
}


}
mwilczynski
  • 3,062
  • 1
  • 17
  • 27

4 Answers4

0

Well, PlayerHealth is a class and the instance variable playerHealth is of type PlayerHealth.

I guess what you really want is

HealthParam.setValue(playerHealth.currentHealth);

which would access the instance variable instead of the class. That's what the last error message suggests.

By using PlayerHealth.currentHealth you suggest that there is a class member (static) named currentHealth, which obviously does not exist. Using playerHealth.currentHealth you access the value of the current so called instance of PlayerHealth.

Short "semi-developer" version of static vs. non-static

Every member variable of a class can either be a class variable (declared static) or an instance variable (without static). A class variable can be accessed without having a current instance of the class (created with new). An instance variable (as the name already suggests) is only available if you have an instance of the class at hand.

Example:

public class StaticVSNonStatic
{
    public static int StaticValue;
    public int NonStaticValue;
}

The following works:

StaticVSNonStatic.StaticValue = 5;

The following doesn't:

StaticVSNonStatic.NonStaticValue = 5;

For the latter you need an instance of the StaticVSNonStatic class:

StaticVSNonStatic instance = new StaticVSNonStatic();
instance.NonStaticValue = 5;

Please note that class variables should really be an exception, as usually the point of having instances of a class is to have separate "records" that follow the same data model but contain different data.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Thank you for that explanation. Technically, this is why the code was not compiling. However, there was a bigger problem that was not allowing the code to function as I intended. Even though changing "PlayerHealth" to "playerHealth" fixed the errors, the code was still not utilizing the correct value of "currentHealth". Hence, the music was not changing in relation to the player's health. – Thomas Sulkoske Jan 17 '16 at 01:56
  • ^The reason for this, and I am still baffled about this, is that there were TWO "PlayerHealth" scripts. Because this was a tutorial project, the original developers included 2 copies of every script in the game. Hence, when calling "PlayerHealth" in my MusicManager script, it would call the script that was not in use in the scene. The version of "PlayerHealth" that was attached had a namespace in the code, and I simply had to include that namespace when calling the script. Even though it was a simple solution, it was a real pain to figure out. – Thomas Sulkoske Jan 17 '16 at 02:00
0

The description of the error says it all. You're trying to access an instance member without providing an object reference in your line.

HealthParam.setValue (PlayerHealth.currentHealth);

I don't know what your PlayerHealth code looks like, but I think you need to change this to:

HealthParam.setValue (playerHealth.currentHealth);

Just make sure your playerHealth variable is properly initialized before using it (you have the initialization commented out in the awake method).

george.zakaryan
  • 960
  • 1
  • 6
  • 18
0

I think you want to change your Update function to the following:

void update() {
    HealthParam.setValue (playerHealth.currentHealth);
}
Steve Cox
  • 381
  • 2
  • 8
0

Questions: Are you trying " HealthParam.setValue(currentHealth)" where "currentHealth" is an 'int' field of class "MusicManager" ? if not, does class "PlayerHealth" has a public field "currentHealth"? Can you show the APIs if class "PlayerHealth" ? if yes, see how to convert int to float for converting 'int' to 'float'

ming R
  • 16
  • 1
  • 1