here is my player script… like i said when i start the game and click on the enemy i want to attack i get a null ref on line 23 of game manager which is line 23 is player.mytarget = hit.transform, and when i click outside of the enemy anywhere in the game i get another null ref on game manager line 27 which is line 27 is player.mytarget = null; im going to post some scripts and hopefully someone can point me in the right direction. if you need more info please let me know… ive been coding for like 2 months.![]()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayerMaybe : Character
{
[SerializeField]
private Block[] blocks;
private bool playerMoving;
private Animator anim;
private static bool playerExists;
private bool attacking;//is player attacking
private bool specialAttacking;
public float specialbuttonAttackTime;
public float attackTime;//how llong player wil attack
private float SBattackTimeCounter;
private float attackTimeCounter;//attack time
private Rigidbody2D myRigidBody;
public Vector2 LastMove;
[SerializeField]
private GameObject[] spellPrefab;
public Transform inpoint;
public float shootingForce;
private Transform targetPos;
[SerializeField]
private Transform[] exitPoiints;
private int exitIndex = 2;
public string startPoint;
//private Transform target;
public Transform MyTarget { get; set; }
// Start is called before the first frame update
void Start()
{
//target = GameObject.FindGameObjectWithTag("Target").transform;
anim = GetComponent<Animator>();
myRigidBody = GetComponent<Rigidbody2D>();
if (!playerExists)//doesnt exist
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);//make sure player will stay in existence when loading worlds
}
else//if it does exist
{
Destroy(gameObject);
}
}
// Update is called once per frame
protected override void Update()
{
GetInput();
base.Update();//exec char update
//Debug.Log(LayerMask.GetMask("Block"));
}
private void GetInput()
{
playerMoving = false;
if (!attacking)
{
direction = Vector2.zero;//after every loop reset
if (Input.GetKey(KeyCode.W))
{
exitIndex = 0;
direction += Vector2.up;
playerMoving = true;
LastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetKey(KeyCode.A))
{
exitIndex = 3;
direction += Vector2.left;
playerMoving = true;
LastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetKey(KeyCode.S))
{
exitIndex = 2;
direction += Vector2.down;
playerMoving = true;
LastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetKey(KeyCode.D))
{
exitIndex = 1;
direction += Vector2.right;
playerMoving = true;
LastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetKey(KeyCode.LeftShift))
{
Attack();
}
if (!specialAttacking)
{
if (Input.GetKeyDown(KeyCode.X))
{
Block();
playerMoving = false;
if ( MyTarget != null && !attacking && inLineOfSight())
SpecialButton();
}
}
//abs value is it 0 or 1 checking to see if movement hor or ver movevement happeneing on both then we know we want move speed slowed for diag direction
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f && Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f)//check to see if any movement if its true then we want to slow downtakes away plus or minus so only 0 or 1
{
currentMoveSpeed = speed * diagMoveModifier;
}
else
{
currentMoveSpeed = speed;//not moving in diag so not slowed
}
}
if (SBattackTimeCounter > 0f)
{
SBattackTimeCounter -= Time.deltaTime;
}
if (SBattackTimeCounter <= 0f)
{
specialAttacking = false;
anim.SetBool("special", false);
}
if (attackTimeCounter > 0f)
{
attackTimeCounter -= Time.deltaTime;
}
if (attackTimeCounter <= 0f)
{
attacking = false;
anim.SetBool("Attack", false);
}
//animator stuff
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("playerMoving", playerMoving);
anim.SetFloat("LastMoveX", LastMove.x);
anim.SetFloat("LastMoveY", LastMove.y);
}
public void Attack()
{
attackTimeCounter = attackTime;
Debug.Log("Attacking");
attacking = true;
myRigidBody.velocity = Vector2.zero;// x y with a 0
anim.SetBool("Attack", true);
}
public void StopAttack(int spellIndex)
{
anim.SetBool("special", false);
}
public void CastSpell(int spellIndex)
{
//Instantiate(spellPrefab[0], inpoint.position, inpoint.rotation);
GameObject spell = Instantiate(spellPrefab[spellIndex], exitPoiints[exitIndex].position, Quaternion.identity);
targetPos = GameObject.FindGameObjectWithTag("Target").transform;
Vector2 tpot = targetPos.position - transform.position;
spell.GetComponent<Rigidbody2D>().AddRelativeForce(tpot * shootingForce, ForceMode2D.Impulse);
blocks[exitIndex].Deactivate();
}
public void SpecialButton()
{
SBattackTimeCounter = specialbuttonAttackTime;
attacking = true;
myRigidBody.velocity = Vector2.zero;// x y with a 0
CastSpell(0);
anim.SetBool("special", true);
Invoke("StopAttack(0)", 1f);//calls stop attack after 1f
}
private bool inLineOfSight()
{
Vector2 targetDir = (MyTarget.transform.position - transform.position).normalized;
Debug.DrawRay(transform.position, targetDir, Color.red);
RaycastHit2D hit = Physics2D.Raycast(transform.position, targetDir, Vector2.Distance(transform.position, MyTarget.transform.position),512);
if (hit.collider == null)
{
return true;
}
return false;
}
private void Block()
{
foreach (Block b in blocks)
{
b.Deactivate();
}
blocks[exitIndex].Activate();
}
}
here is my gameManager script :
[SerializeField]
private PlayerMaybe player;
void Update()
{
ClickTarget();
//Debug.Log(LayerMask.GetMask("Clickable"));
}
private void ClickTarget()
{
if (Input.GetMouseButtonDown(0))//left mouse button right click is 1
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, 1024);
if (hit.collider != null)
{
player.MyTarget = hit.transform;
}
else
{
player.MyTarget = null;
}
}
}
}
here is my spell script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spell : MonoBehaviour
{
private Rigidbody2D myRB;
[SerializeField]
private float speed;
public int damage;
public GameObject damageNumber;
private HealthSystem healthSystem;
public GameObject damageBurst;
public Transform hitPoint;
private Transform target;
// Start is called before the first frame update
void Start()
{
myRB = GetComponent<Rigidbody2D>();
target = GameObject.FindGameObjectWithTag("Target").transform;
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D other)
{
if (other.transform == target)
{
Destroy(gameObject);
Instantiate(damageBurst, target.position, target.rotation);
var clone = (GameObject)Instantiate(damageNumber, other.transform.position, Quaternion.Euler(Vector3.zero));//creating obj for use in game,
clone.GetComponent<floatingNumbers>().damageNumber = damage;
}
}
}
like i said im very new to coding so if someone could please help me out that would be awesome!