Problems with variables across scripts

First and foremost, thank you for reading this question. I have already viewed the “Accessing variables across scripts” Official YOUTUBE Video many many times. I have also read many questions and answers on this forum hoping to glean some information that may help. Still having problems, you all are my last hope.

I think I may not be understanding some of the relationships involved between classes.

HERE are SCREENSHOTS of my Editor/Inspector proving that my PREFABS should be linked properly:

alt text alt text

LOGIC:

  1. Fist I set GameController as static. I understand this allows it to have global variables that other classes can access and modify.
  2. Then, I generate 6 different spheres with different colors.
  3. Then, the game increases the Z position for each cloned sphere, until >4, then it switches the direction so the spheres are supposed to come back. (Z position declination)
  4. And while the Spheres, do correctly Instantiate() on the screen, and move forward on the Z axis…

THE PROBLEM:

  1. is that the spheres continue on the +Z axis forever. They never switch and come back
  2. In GameController script, the print function for refscript.tempVector always prints “0,0,0”
  3. It’s as if Gamecontroller never received the appropriate value.

#GameController.cs
sing UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {


	public static GameController controller; // Allows this script to store the base values and this is now the holder if such values.

	//Loading Script use
	public Material greenMaterial;
	public Material blueMaterial;
	public Material whiteMaterial;
	public Material redMaterial;
	public Material pinkMaterial;
	public Material yellowMaterial;
	public float speedInterval;

	// For referencing the vales on ObjectBehavior.cs
	public GameObject objectContainingScript;						// PUBLICVAR (its a GameObject) (insert in the editor which object contains the script)
	

	// GENERAL DEFINITIONS (HIDDEN IN INSPECTOR)
	[HideInInspector]public Objectbehavior scriptref;				// PUBLICVAR (The Class ObjectBehavior) (set it as scriptref)
	[HideInInspector]public Material materialToUse;
	private GameObject sphere;
	private float myX;
	private Vector3 tempVector;





	void Awake (){
		if (controller == null)
		{
			DontDestroyOnLoad (gameObject);
			controller = this;
		}
		else if (controller != this) 
		{
			Destroy(gameObject);		
		}

	}


	//THIS SCRIPT



	// Use this for initialization
	void Start () {
		sphere = objectContainingScript;										// its the same object so we can use it.

            scriptref = objectContainingScript.GetComponent<Objectbehavior> ();
		int ballType = 1;														// greenMaterial by default
		for (myX = 0; myX < 6; myX++) 
		{
			tempVector[0] = myX*2;
			tempVector[1] = 0;
			tempVector[2] = 0;
			ballType = (int)Random.Range (1f,7f);
			if (ballType == 1){materialToUse = greenMaterial;}
			if (ballType == 2){materialToUse = redMaterial;}
			if (ballType == 3){materialToUse = yellowMaterial;}
			if (ballType == 4){materialToUse = blueMaterial;}
			if (ballType == 5){materialToUse = pinkMaterial;}
			if (ballType == 6){materialToUse = whiteMaterial;}
			sphere.renderer.material = materialToUse;							// set the clone sphere's color to the color necessary
			Instantiate (sphere, tempVector, transform.rotation );
		}
	}
	
	// Update is called once per frame
	void Update () {
		values inside of the object's script <ObjectBehavior>
		print (scriptref.mytempVector);											//scriptref.tempVector always = 0,0,0 @ RUNTIME , YET THE BALL MOVES FORWARD
		if (scriptref.mytempVector[2] > 4) {scriptref.direction = -1;}			// I am trying to check that balls Z position, and if exceeeds parameter then switch it's direction
		if (scriptref.mytempVector[2] < 0) {scriptref.direction = 1;}			// ****** it autocompletes the variable like there is a connection, but then it keeps returning 0,0,0
																				// The editor Scene view proves the balls move outward in a positive Z direction
	}
}

#ObjectBehavior.cs
using UnityEngine;
using System.Collections;

public class Objectbehavior : MonoBehaviour {



	public int direction;
	[HideInInspector]public Vector3 mytempVector;
	
	[HideInInspector]public float stepInterval;



	// Use this for initialization
	void Start () 
	{

		gameObject.tag = "temp";
		direction = 1;
		stepInterval = GameController.controller.speedInterval;








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

		mytempVector = transform.position;													//
		if (direction == 1) {mytempVector [2] = mytempVector [2] + stepInterval;}			//
		if (direction == -1) {mytempVector [2] = mytempVector [2] - stepInterval;}			//
		transform.position = mytempVector;
		print (mytempVector);																// Proves it is moving in a positive Z direction initially , but never comes back
	
	}
}

No you don’t make scripts static, you should avoid making variables static where possible and in this case you don’t need to.

What is the GameController script attached to in the hierarchy?

Whatever it is find that object and then use “THATOBJECT”.getComponent().speedInterval;

let’s say you’ve got an empty gameObject called GC that your GameController is attached to then you’d set your stepInterval to the public speedInterval like this

stepInterval = Find("GC").GetComponent<GameController>().speedInterval;

public is all you need for the variable to be accessible but you need to identify which script you’re referencing.

Think about this, if you have a health script attached to enemies then you’d use the same script for all enemies. If you attack one enemy then you identify that enemy by a collision with your character and find the health script component attached to that specific enemy.

That’s why you have to find where the script is attached and get the GameController script. It’s not obvious because you’ll likely only have 1 GameController but it still follows the same principals.