script type variables

i often notice while developing that developers use: public scriptname varname and the script they reference are blank scripts! please explain its functionality, use and what does it do etc. cannot find it anywhere.

There are multiple uses to this method and then more giving that programming ideas are endless. But I’m just going to list two in order to give you an idea.

1.One use of this would be to make a one way script and ensure there is only one of the so called script in your game. This could be used for highscores or game controllers. This below is an example of a script I’ve used with the name SaveGame…

	public static SaveGame control;

	void Awake() 
	{
		//make sure it doesnt spawn multiple of these...
		if (control == null) {
			DontDestroyOnLoad (gameObject);
			control = this;
		} else if (control != this) {
			Destroy (gameObject);
		}

This method here checks to see if there are any “Controls” in the scene, and if there are it destroys the gameObject attached to the script. I’m using it to save and load my games.

2.Another use for this would be to easily identify enemies in front of you. You could call a raycast to always be ahead of you and check if the game object in front contains an enemy health script. Thankfully I’ve used something similar.

Like this:

if (Physics2D.Raycast (transform.position, Vector2.right * -1, range, WhatIsntMe)) {
	if (rHit.transform.GetComponent <enemyEssentials> () != null) {
		enemyDiscovered = true;
		enemy = rHit.transform.enemyEssentials;
	}
}

This could simply link you to the enemy essentials and all you would have to do is call a function that allows you to take damage to the enemy, or the other way around.

There really are infinite possibilities with this. I suppose when it comes to the empty script, it might be cleaner to put variables in a different script. Or perhaps they could be using it for easy conversion of Java to C# (not quite sure if it’ll work that way).
I’m assuming they would’ve written in the separate code in the future if it really was empty, but these are just a few examples I could think of.

If this answer was helpful, feel free to give it an upvote and consider it “the answer” :smiley:

  • Seth Albertus

is the savegame script empty and what is the meaning of control = this? please help! @Sethhalocat