- Home /
Enemy Targetting/Player Attack Problem
I have two scripts, playerAttack.cs and Targetting.cs
playerAttack handles the player doing damage to an enemy target, and Targetting handles cycling through an array of potential targets. They both work fine independently, however trying to set the current target variable in the playerAttack script equal to the selected target variable in the Targetting script has been a problem for me. Can someone enlighten me on how I can set the curTarget to the selectedTarget in the playerAttack script?
This first script was successful in setting the curTarget equal to the selectedTarget, but threw an error:
NullReferenceException: Object reference not set to an instance of an object playerAttack
public class playerAttack : MonoBehaviour { // The player's current current target public GameObject curTarget;
// Update is called once per frame
void Update () {
GameObject go = GameObject.Find ("pc");
Targetting tg = go.GetComponent();
curTarget = tg.selectedTarget.gameObject;
}
This second script is my failed attempt at setting an instance of the object to get rid of the above error, perhaps this is where I need help?
public class playerAttack : MonoBehaviour { // The player's current current target public GameObject curTarget;
// Update is called once per frame
void Update () {
GameObject goT = GameObject.Find ("pc");
Targetting tg = goT.GetComponent();
GameObject go = Instantiate(
goT,
goT.transform.position,
Quaternion.identity)
as GameObject;
curTarget = tg.selectedTarget().gameObject;
}</pre></code>