Asign values to 3 public variables from(ScriptA)GET vaiables from (ScriptB)-UNITY c#

Hi everyone.

Im not sure if this is the right place to ask .

Lets say that i have one script called DEPTH with 3 Float variables .

Code:
public class DEPTH: MonoBehaviour
{
public float PosiX;
public float PosiY;
public float PosiZ;

void onGUI()
{
PosiX= firstP.x /texture.widthScreen.width ;
PosiY= firstP.y/texture.height
Screen.height;
PosiZ= 0;
}
}

Then my ScriptB called: AutoSpawn.
The script is excuted every 10 seconds and i think im referencing DEPTH Script to script to get the variables.
But i get Null everytime.

Código:
Class SpawnEnemies (){
public float PosiXX;
public float PosiYY;
public float PosiZZ;

DEPTH script;

void Start () {
PosiXX=script.PosiX.ToString;
PosiYY=script.PosiY.ToString;
PosiZZ=script.PosiZ.ToString;
}
}

Can anybody please givme a tip.

Thank you all.

If want to reference another script, you have to use GetComponent. Check the Unity Script Reference.

Or else on your ScriptB AutoSpawn instead of script DEPTH you write :

[SerializeField] private DEPTH my_depthObject ;
OR
public DEPTH my_depthObject ;

I like to private everything I can but that’s just me.

and your start function become

void Start () {
PosiXX=my_depthObject.PosiX.ToString;
PosiYY=my_depthObject.PosiY.ToString;
PosiZZ=my_depthObject.PosiZ.ToString;
}

Don’t forget to add your object in the editor or you’ll get a null ref.