I’m working on the mechanics of my Cleric class, where I’m trying to instantiate particle effects to the targeted player/enemy position. But that aspect isn’t working. I’m getting a null error.
using UnityEngine;
using System.Collections;
public class ClericAbilities : MonoBehaviour
{
public ParticleSystem smiteParticle;
public ParticleSystem smiteTargeted; //UNDER RESOURCES FOLDER AS A PREFAB AS "Smite"
public float speed;
public ParticleSystem healParticle;
public ParticleSystem healTargted; //UNDER RESOURCES FOLDER AS A PREFAB AS "Heal"
public ParticleSystem resurrectionParticle;
public ParticleSystem resurrectionTargeted; //UNDER RESOURCES FOLDER AS A PREFAB AS "Resurrection"
private bool isPlayerTargeted;
private bool isEnemyTargeted;
private Vector3 currentPlayerPosition;
private Vector3 currentEnemyPosition;
void Awake()
{
isPlayerTargeted = false;
isEnemyTargeted = false;
}
// Use this for initialization
void Start ()
{
animation["Attack"].layer = 1;
animation["Heal"].layer = 2;
animation["Resurrection"].layer = 3;
smiteTargeted = Resources.Load("Smite") as ParticleSystem;
healTargted = Resources.Load("Heal") as ParticleSystem;
resurrectionTargeted = Resources.Load("Resurrection") as ParticleSystem;
}
void Target()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 1000))
{
string hitTag = hit.transform.tag;
if (hitTag == "Player")
{
isPlayerTargeted = true;
currentPlayerPosition = hit.transform.position;
Debug.Log("Player selected");
}
else
{
isPlayerTargeted = false;
}
if(hitTag == "Enemy")
{
isEnemyTargeted = true;
currentEnemyPosition = hit.transform.position;
Debug.Log("Enemy selected");
}
else
{
isEnemyTargeted = false;
}
}
}
}
void Attack()
{
//1 second cast time
if(Input.GetMouseButtonDown(1) && isEnemyTargeted)
{
animation.CrossFade("Attack");
smiteParticle.Play();
ParticleSystem smite = Instantiate(smiteTargeted) as ParticleSystem; //NULL ERROR
smiteTargeted.Play();
smite.transform.position = currentEnemyPosition;
}
}
void Heal()
{
if(Input.GetKeyDown(KeyCode.Alpha2) && isPlayerTargeted)
{
//3 second cast time
animation.CrossFade("Heal");
healParticle.Play();
ParticleSystem heal = Instantiate(healTargted) as ParticleSystem; //NULL ERROR
healTargted.Play();
heal.transform.position = currentPlayerPosition;
}
}
void Resurrection()
{
if(Input.GetKeyDown(KeyCode.Alpha3) && isPlayerTargeted)
{
//5 second cast time
animation.CrossFade("Resurrection");
resurrectionParticle.Play();
ParticleSystem resurrection = Instantiate(resurrectionTargeted) as ParticleSystem; //NULL ERROR
resurrectionTargeted.Play();
resurrection.transform.position = currentPlayerPosition;
}
}
// Update is called once per frame
void Update ()
{
Target();
Attack();
Heal();
Resurrection();
}
}