I have a script that handles moving an object, with a few private variables such as movUp, movDown, etc. as well as methods moveUp, moveDown and so on which set the corresponding variable to 1. I also have a print command in both the methods and the fixedUpdate method which simply prints out all 4 variables. The print in fixedUpdate always prints “0000”, regardless of whether a move method was called, but the truly weird thing is that the print in the mov methods not only displays the changed variable, for example “1000” when movUp is called, but if I then stop the game, change the method to only print the variables without actually changing them and run the game again, it STILL prints “1000”, even though the second time I run it there is not a single line which changes movUp, and despite all of this, the print method in fixedUpdate STILL prints “0000”. It’s almost as though the 2 methods in 1 script are accessing completely different, but identically named variables which are, in addition to that, persistent from game to game.
Edit: Here’s the code, there isn’t much though.
using UnityEngine;
public class MoveScript : MonoBehaviour
{
public Vector2 speed = new Vector2(1, 1);
private Vector2 movement;
private bool moving = false;
private bool stopped = false;
private float movUp = 0;
private float movDown = 0;
private float movLeft = 0;
private float movRight = 0;
public void moveUp(){
movUp = 1;
print ("movUp");
print("Update"+movUp+movDown+movLeft+movRight);
}
public void moveDown(){
movDown = 1;
print("Update"+movUp+movDown+movLeft+movRight);
}
public void moveLeft(){
movLeft = 1;
print("Update"+movUp+movDown+movLeft+movRight);
}
public void moveRight(){
movRight = 1;
print("Update"+movUp+movDown+movLeft+movRight);
}
void FixedUpdate()
{
print("Variables"+movUp+movDown+movLeft+movRight);
}
}
Oh right, it sounds like you are doing the prefab reference wrong. In general circumstances, you really only assign a prefab to a script if you plan to call “Instantiate(myPrefabRef)” inside that script. This would create a prefab. By assigning the prefab to your script, it is accessing the prefab instance, not the instance in the game.
Instead, there are several alternatives. I assume that you have some “Manager” or “Input Handler” script that you mentioned above that calls the “moveUp()”, etc. methods on the cube. You were calling that method on the prefab object. You want to call it on the cube in the game environment instead.
The most direct approach is to assign the object from the game environment to the “Manager” script (or whichever script calls the moveUp, etc, methods) instead of assigning the prefab. If you aren’t creating the cube in the editor, before you hit play, then you will need a more dynamic approach.
Your Manager class could provide a static method “AddReference(CubeClass instance)”. Then the Cube class, in the “void Start()” method could add itself to the manager. Something like this (some of this may be psuedocode).
class Manager (extends mono, etc)
{
static List<CubeClass> _cubeRefs;
public static void AddReference(CubeClass ref)
{
_cubeRefs.Add(ref);
}
public static void RemoveReference(CubeClass ref)
{
_cubeRefs.Remove(ref);
}
void SomeMethod()
{
for (int i=0; i < _cubeRefs.length; ++i)
{
_cubeRefs*.moveUp();*
}
}
}
class CubeClass (extends mono, etc)
{
void Start()
{
Manager.AddReference(this);
}
void KillThisCube()
{
Manager.RemoveReference(this);
Destroy(gameObject);
}
}
This is just psuedocode, it won’t compile, but it gives you an idea of how to get references to something that is created at runtime. If you have multiple cubes in the environment but just want to select and move one cube, you obviously wouldn’t loop through them all like the code above. In that case, you could add a new function called “public static void SetActiveCube(CubeClass instance)” and just save off the instance for calling its methods later on.