Search code examples
c#unity-game-enginemonodevelopfmod

Cannot convert `object' expression to type `float' - HealthParam.SetValue (PlayerHealth.currentHealth);


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


}

Solution

  • 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.