Static GameObject Variable

I have a quick question that i have wondered about for quite some time. You can normally set a static variable with the following code

static var num = 20;

This works unless you have to drag an object (a gameobject, rigidbody, light, etc) into the inspector to assign it. for instance:

if you wanted to set your player variable you would code

var player : GameObject;

This would work unless you wanted to make that player reachable throughout multiple scripts

static var player : GameObject;

This doesn’t show up in the inspector. Not that i need this for my project, I was just wondering if there was any way to have a static GameObject variable without taking up 3 lines to say :

var player : GameObject; // the following is assigned in the inspector
static var staticplayer : GameObject; //this is the static version of the variable
staticplayer = player; // the static version now equals the version in the inspector

Simply a curiosity “What if” question. Thanks for taking the time to read this

“var player : GameObject; This would work unless you wanted to make that player reachable throughout multiple scripts”

Is very untrue. To access something that is public but not static, use GetComponent.
Say you have a script on your player with the Player variable set. Then you have some other script that wants to access the Player variable in the sense that it is not static. You need to have some reference to your object that has the player script on it, then access the script (component). I’m not sure why you want your objects to be static, but if you do,

1)They won’t show up in the inspector.

2)There can only and always be ONE instance of the object.

3)It’s easy to lose data if you’re not certain on what you’re doing with static variables.

The only time I really ever use static functions, classes, or variables is for Editor scripts.

Example on how to use GetComponent:

//PlayerScript.js

var Player : GameObject;
var num : int = 10;

//SomeOtherScript.js

#pragma strict

private var ObjectWithPlayerScript : GameObject;
var playerScriptNum : int;

function Start () 
{
	ObjectWithPlayerScript = GameObject.Find("Player");
	//Say you want to change var num from PlayerScript:
	playerScriptNum = ObjectWithPlayerScript.GetComponent(PlayerScript).num;
	playerScriptNum += 10;
	print(playerScriptNum);
}