Hi I am having problems getting my script component out of my game object.
I have assigned GameObjects “Defender” to my buttons. Made public static GameObject “currentDefender” to have easy access to it form another script.
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour {
SpriteRenderer fadeAlpha;
Color currentColor = Color.white;
Button[] buttonArray;
public GameObject Defender;
public static GameObject currentDefender;
// Use this for initialization
void Start () {
buttonArray = GameObject.FindObjectsOfType<Button>();
currentDefender = Defender;
}
Here i am trying to access Deffender script attached to Defender Object above:
public class DefenderSpawner : MonoBehaviour {
public Camera myCamera;
GameObject parent;
Deffender defenderScript;
void Start()
{
parent = GameObject.Find ("Defenders");
if (!parent)
{
parent = new GameObject("Defenders");
}
}
void OnMouseDown ()
{
Vector2 rawPos = CalculateWorldPointOfMouseClick();
Vector2 roundedPos = SnapToGrid (rawPos);
GameObject defender = Button.currentDefender;
defenderScript = defender.GetComponent<Deffender>();
GameObject newDef = Instantiate(defender, roundedPos, Quaternion.identity) as GameObject;
newDef.transform.parent = parent.transform;
Debug.Log(defenderScript);
}
And my defenderScript keeps returning null. Can anyone please explain to me what is going on .Thank you.