Transform component not showing in inspector

Hi all,

I cant seem to figure this one out. The transform component called opponent doesnt show up in my inspector to add my enemy object. It only happens when the transform opponent is static. I need to access it in the enemy object. This is early days of my project so irriating to say the least. The logic is correct.

PLAYER

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	public string playerName = "Player one";
	public int health;
	public int damage;
	public float range;
	public static Transform opponent;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		Attack();
	}

	void Attack(){
		if(Input.GetMouseButtonUp(1)){
		//if(Input.GetKeyUp(KeyCode.Space)){
			if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range){
				opponent.GetComponent<Enemy>().GetHit(damage);
			}
		}
	}
}

ENEMY

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	public string enemyName = "First Enemy";
	public int health;
	public int damage;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void GetHit(int playerDamage){
		health = health - playerDamage;
	}

	void onMouseOver(){
		Player.opponent = transform;
	}
}

Like maccabbe said in a comment, static variables just don’t show up in the inspector.

The inspector is an editor dialog to alter the state of a specific object. Static variables are not for a specific object, even if there is only ever one single instance of your Player around.

What you can do instead, is make the opponent non-static and add a second variable named like… “public static Player thePlayer;” to your player. You set this variable in OnEnable and clear it in OnDisable.

This way, other components can still access the players opponent using “Player.thePlayer.opponent”. This construct is often called a “singleton”, “global getter”. (Other people call it “the root of all evil” but thats for another book ;))

public class Player : MonoBehaviour {
    public string playerName = "Player one";
    public int health;
    public int damage;
    public float range;
    public Transform opponent;
    public static Player thePlayer;

    void OnEnable() { thePlayer = this; }
    void OnDisable() { thePlayer = null; }
}

public class Enemy : MonoBehaviour {
    ...
    void onMouseOver(){
        Player.thePlayer.opponent = transform;
    }
}

public static properties won’t be expose in the inspector. Make an instance of Player and access opponent that way.

Player player = (Player) GameObject.FindObjectOfType(typeof(Player));
player.opponent = transform;     //opponent being not static