So I have a prefab item in Unity called GameFrame with the script GameFrame attached to it. I have 2 instances of this prefab in my scene and for some reason the variable firstSelectedBlock is behaving as if it was static. The code swaps two blocks when you click on two that are right next to each other. When you click a block that ISN’T right next door it selects that block instead. Everything works fine. happy fun times.
HOWEVER, if I create 2,3,4 instances of the GameFrame prefab if I click a block in ANY of them it sets the firstSelectedBlock variable in all of them. The variable was originally private but I switched it to public just so I could watch it at run time easily and sure enough they are all synchronized at all times.
Am I missing something silly? I’m really lost on this one.
Thanks in advance for any help… code below.
using UnityEngine;
using System.Collections;
public class GameFrame : MonoBehaviour {
public GameObject blockLifter;
public GameObject[] basicBlocks;
public LayerMask gameBlockMask;
private Vector2 blockSpacing = new Vector2(21,20);
private int levelHeight=5;
private int gameWidth=6;
private float xStartPosition;
private GameObject currentLifter;
private int liftSpeed=2;
private bool liftPaused=true;
private GameObject trackThisOne;
public GameObject firstSelectedBlock;
private bool canSelectBlocks=false;
void Start () {
xStartPosition=-((gameWidth / 2) * blockSpacing.x);
SetupNewGame();
}
// Update is called once per frame
void Update () {
//if we have a click
if(Input.GetMouseButtonDown(0) && canSelectBlocks){
//lets cast a ray and see if they clicked a block
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//if they did lets see which one
if(Physics.Raycast(ray, out hit, 1000f, gameBlockMask)){
GameObject hitObj = hit.collider.gameObject;
//if they already have a block selected lets swap them
//and that block is compatible for swapping (on same line AND right next door)
if(firstSelectedBlock && hitObj &&
Mathf.Abs(firstSelectedBlock.transform.position.y - hitObj.transform.position.y) < 1 &&
Mathf.Abs(firstSelectedBlock.transform.position.x - hitObj.transform.position.x) < blockSpacing.x + 1){
doSwapBlocks(firstSelectedBlock,hitObj);
}else{
if(firstSelectedBlock)
firstSelectedBlock.transform.position = new Vector3(firstSelectedBlock.transform.position.x,firstSelectedBlock.transform.position.y,0);
//otherwise lets select a block
firstSelectedBlock=hitObj;
hitObj.transform.position = new Vector3(hitObj.transform.position.x,hitObj.transform.position.y,blockSpacing.y * 0.75f);
}
}
}
}