0
\$\begingroup\$

I am attempting to make a global MaterialAssign script that I can assign to anything and have an enum selector in the inspector to assign a material type. This would then drive hitFX and soundFX variation in other scripts.

My selector script MaterialAssign.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaterialAssign : MonoBehaviour
{
    public enum MatSelection
    {
        defaultMat,
        metalEnemy,
        metalWall,
        dirtWall
    }
    public static MatSelection currentMat;
}

Attempt at usage in another script:

if (MaterialAssign.currentMat == MaterialAssign.MatSelection.metalEnemy)
{
    selectedHitFX = bulletMetalEnemyFX;
}
else if (MaterialAssign.currentMat == MaterialAssign.MatSelection.metalWall)
{
    selectedHitFX = bulletMetalWallFX;
}

I have no errors in the console BUT the enum selector doesn't show up in the inspector, so I'm assuming no value is ever passed. I read that enums can't have a static value, but not sure how to handle that.

Any assistance would be most appreciated or alternate approaches welcome! I'm purposely trying to stay away from tags, but can go down that path if needed.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

the enum selector doesn't show up in the inspector

That's because currentMat is a static field. Static fields never show in the inspector, because they are not associated with a specific class instance.

\$\endgroup\$
3
  • \$\begingroup\$ If I just remove static then there are other errors that pop up, I'm out and about so I'll have to get those errors when I get back. \$\endgroup\$
    – benbastien
    Feb 11, 2021 at 23:10
  • 1
    \$\begingroup\$ The other errors are probably telling you that you need to have an instance to read the currentMat field from. Be sure to get the instance of the MaterialAssign component attached to the object whose material you want information about. \$\endgroup\$
    – DMGregory
    Feb 11, 2021 at 23:13
  • \$\begingroup\$ All is well now. I have the following working code: public class MaterialAssign : MonoBehaviour { public enum MatSelection { defaultMat, metalEnemy, metalWall, dirtWall } public MatSelection currentMat; } and then calling it in with MaterialAssign mat = bulletHit.transform.GetComponent<MaterialAssign>(); //Debug.Log(mat.currentMat); if (mat.currentMat == MaterialAssign.MatSelection.metalEnemy) Thanks for the help guys. \$\endgroup\$
    – benbastien
    Feb 12, 2021 at 1:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .