- Home /
Solved.
Variable changes back to default after Start()
Hello,
I am working on a 2D mobile game where the player object movement is restricted to left/right in the x-axis. Movement is also set to line up with specific points along the x-axis that correspond with columns of enemies, that way when the player object fires it will always hit an enemy. I have touch pads along the x-axis of the player object that line up with those columns as well, the pads have a touch script that should make the player object move to that pad and fire when the player touches that pad. The problem is that sometime between Start() and when the player touches the pad, the variable that holds the information about which column that pad lines up with resets to 0. These are prefabs by the way, instantiated during the start of the level in a separate game controller script and the variable is set by a function call right after the pad object is instantiated.
The GameController code that instantiates the touch pad and sets it's column variable to i:
//the firePoints[i] array holds vector3 spawn points for the player movement touch pads
for(int i = 0; i < columns; i++){
GameObject temp = Instantiate(firePoint, firePoints[i], Quaternion.identity) as GameObject;
Debug.Log("i = " + i);
temp.GetComponent<PlayerTouchPad>().SetColumn(i);
}
The PlayerTouchPad class:
public class PlayerTouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
int fireColumn;
bool isTouched;
int touchID;
GameObject player;
void Awake(){
isTouched = false;
Debug.Log("Awake firecolumn = " + fireColumn);
}
void Start(){
player = GameObject.FindGameObjectWithTag ("Player");
Debug.Log("Start firecolumn = " + fireColumn);
}
public void OnPointerDown(PointerEventData data) {
if (!isTouched) {
isTouched = true;
touchID = data.pointerId;
}
}
public void OnPointerUp(PointerEventData data) {
if (data.pointerId == touchID) {
player.GetComponent<PlayerController>().Mover(fireColumn);
}
isTouched = false;
}
public void SetColumn(int column){
fireColumn = column;
}
The PlayerController Mover() method:
public void Mover (int column)
{
int move = 0;
Debug.Log ("Column = " + column);
if (!inFlight && !isMoving) {
Vector3 direction = controller.firePoints[column];
isMoving = true;
move = (int)direction.x;
//Starts a coroutine to smoothly move the player to the point 'direction'
StartCoroutine (SmoothMove (direction, move));
}
}
The debug line in PlayerTouchPad.Start() outputs the correct column numbers(0, 1, 2, 3, 4 in the case of 5 columns, which is as expected). However when the PlayerController.Mover(int) script is called in the OnPointerUp method, the debug output in the Mover method returns 0, no matter which pad I touch.
I am really not sure why that variable keeps resetting, I did some searching and could not find an answer, but my search-fu may have been weak.
Any suggestions are welcome, thank you.
Answer by cwnichols · Sep 22, 2015 at 07:11 AM
Turned out I had somehow attached the script to a UI object without realizing it, it was invisible during game play, and it was the thing intercepting touches. Closing this now.
Follow this Question
Related Questions
Is it possible to show Static Variables in the Inspector? 10 Answers
prefabs not loading script reference via getComponent 1 Answer
How do I assign a prefab to a variable with a script? 1 Answer
Quickest method to change a variable on another gameobject. 2 Answers
Change prefab color when shuriken particle touches it 1 Answer