- Home /
Cannot change the value of this integer that I use for array,Cannot change the value of this one.
public class BuildingButton : MonoBehaviour {
PlacementScript ps = new PlacementScript();
public void setBuildingVal(int selectBuildingVal)
{
ps.selectedObjInArray = selectBuildingVal;
}
}
this one above is my button script
public class PlacementScript : MonoBehaviour {
public int selectedObjInArray;
private GameObject currentlySelectedObj;
[SerializeField]
private GameObject[] selectableObj;
private bool isAnObjcSelected=false;
// Use this for initialization
void Start () {
}
void Update()
{
if (Input.GetMouseButtonDown(1) && isAnObjcSelected == true)
{
Destroy(currentlySelectedObj);
isAnObjcSelected = false;
selectedObjInArray = 0;
}
}
// Update is called once per frame
public void SelectIt () {
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 spawnPos = new Vector2(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));
if (isAnObjcSelected == false)
{
currentlySelectedObj = (GameObject)Instantiate(selectableObj[selectedObjInArray], spawnPos, Quaternion.identity);
isAnObjcSelected = true;
}
}
}
and this one is my placement script. I have different prefabs in that array and I want to be able to click different buttons assigned for different sprites but selectedObjInArray is not changing when I click a button where I set the int value to 1,2,,, What did I do wrong?, public class BuildingButton : MonoBehaviour {
PlacementScript ps = new PlacementScript();
public void setBuildingVal(int selectBuildingVal)
{
ps.selectedObjInArray = selectBuildingVal;
}
}
this one above is for my button. I'. trying to set a new int value for another class
public class PlacementScript : MonoBehaviour {
public int selectedObjInArray;
private GameObject currentlySelectedObj;
[SerializeField]
private GameObject[] selectableObj;
private bool isAnObjcSelected=false;
// Use this for initialization
void Start () {
}
void Update()
{
if (Input.GetMouseButtonDown(1) && isAnObjcSelected == true)
{
Destroy(currentlySelectedObj);
isAnObjcSelected = false;
selectedObjInArray = 0;
}
}
// Update is called once per frame
public void SelectIt () {
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 spawnPos = new Vector2(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));
if (isAnObjcSelected == false)
{
currentlySelectedObj = (GameObject)Instantiate(selectableObj[selectedObjInArray], spawnPos, Quaternion.identity);
isAnObjcSelected = true;
}
}
}
When I click on the button it does not change the value so I can not pick the second object in the array.
Comment