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:
LOGIC:
- Fist I set GameController as static. I understand this allows it to have global variables that other classes can access and modify.
- Then, I generate 6 different spheres with different colors.
- 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)
- And while the Spheres, do correctly Instantiate() on the screen, and move forward on the Z axis…
THE PROBLEM:
- is that the spheres continue on the +Z axis forever. They never switch and come back
- In GameController script, the print function for refscript.tempVector always prints “0,0,0”
- 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
}
}