I’m having a major problem with my code. I have a variable, buttonObject, which is being imported to my DefenderSpawner.cs from Button.cs script correctly. This variable contains the object that should be spawned. Using the Print function, I’ve found that buttonObject is not null and is never altered. However, the error pops up when I try to add my little guys to the Core Game. For anyone who tries to help out, I believe most of the problem lies within the Button.cs script (which is why I included a large portion of Button.cs), but I can’t quite figure this out. Anyone have any tips for me? I’d like the problem explained.
void OnMouseDown(){
Vector2 mousePos = SnapToGrid(wpMouseClick ()); //makes a variable that is wpMouseClick snapped to grid
Instantiate (button.buttonObject, mousePos, Quaternion.identity); //instantiates the selected object at the mousePos converted to actually use game units and is rounded
}
public class Button : MonoBehaviour {
private Button[] buttonArray;
public GameObject buttonObject;
// Use this for initialization
void Start(){
buttonArray = GameObject.FindObjectsOfType<Button>();
}
void Update(){
print (buttonObject);
}
void OnMouseDown () {
Renderer renderer = gameObject.GetComponent<Renderer>(); //on click, set material to clear
foreach (Button selectedButton in buttonArray){
selectedButton.GetComponent<SpriteRenderer>().color = Color.black; //sets all to black
}
GetComponent<SpriteRenderer> ().color = Color.white;
print (buttonObject);
}
}