"Getter and setter" or "public static"?

Hi, i was wondering what’s the best way to handle communication between different object. Usually in Object oriented languages for this purpose you use getter and setter method but what is better in Unity? It will cost more for the processor (and in time response) to use a getter/setter method and call

target.GetComponent<ComponentName>().SetFoo(true);

or to make the variable public static and set the value with dot syntax from other object like:

Component.variableName=true;

thx
Gabriele

There isn’t a best way; you’d use what’s appropriate. Static means there’s only one instance in the class, which sometimes is what you want and sometimes isn’t.

–Eric

yes i know, but i meant just by an “optimizational” point of view. Do you know which method requires less cpu cycles?

If you’re calling GetComponet every frame, a global variable will require less CPU cycles, but if you have a reference to the object and script I assume they’d both perform equally.

As Eric said, sometimes Static/Global variables make sense, sometimes they don’t.

I mostly use them for things like score, and other things you need a single instance of.

CPU cycles isn’t what you should be primarily considering…if you need a static variable, use static, otherwise don’t. You can cache the GetComponent reference if you’re using it repeatedly, but whether you do that or not won’t have any effect on framerate except in fairly extreme cases.

–Eric

Given you are comparing to getters and setters I don’t think you mean to use a static variable, a public instance variable would be a substitute. The main motivation behind getters and setters is that it enables the class to define what happens when these variables change (e.g. you can fire event, validate the new value, etc). Typically accessing a variable would be faster but the overhead of a function call is very small, its unlikely you would have performance issues due to this.

My own rule of thumb is use getters and setters for everything except references to other components that are assigned/visible in the editor. That’s likely because I do most of my coding in Java these days.

thank you!

I ask you one more thing:
For example in this situation if i want to optimize my script avoiding calling GameObject.GetComponent at every trigger interaction but instead i want to reference it with my variable “script”. What type i have to use? I mean where i put “Type???”

using UnityEngine;
using System.Collections;

public class LeftCarSpawner : MonoBehaviour {
	
	GameObject target;
	Type??? script;
	
	//Initialization
	void Awake () {
		target=GameObject.FindWithTag("Spawner");
		script=target.GetComponent<SpawnerController>();
		
		
	}
	
	
	void OnTriggerStay(Collider col){
		if(col.gameObject.tag=="Car"){
			script.SetStart(0, true);
		}
	}
	
	
	void OnTriggerExit(Collider col) {
        if (col.gameObject.tag=="Car"){
			script.SetStart(0, false);
		}
    }
	
}

The type is the name of the script.

–Eric