im learning unity and c# via a pathway(this one) and i’ve been thrown into making homing missles with no prior knowledge of projectliles. i was wondering why this error was showing up. i honestly have no idea whatsoever why the error is there.
the problem im having is that everytime i press “f” to spawn a missle for each enemy.
it only spawns 1 missle and the console gives me the following error:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.misslelin () (at Assets/scripts/PlayerController.cs:78)
PlayerController.Update () (at Assets/scripts/PlayerController.cs:31)
this is my player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody PlayerRb;
public float speed = 5.0f;
public float PowerUpStrenght = 15.0f;
private GameObject FocalPoint;
public bool HasPowerup = false;
public bool hasmissle = false;
public GameObject PowerUpIndicator;
public GameObject misslewarner;
public spauwnar spawner;
public GameObject missle;
public GameObject tmpmislle;
public RocketBehavior rocketBehavior;
void Start()
{
FocalPoint = GameObject.Find("focal point");
PlayerRb = GetComponent<Rigidbody>();
}
void Update()
{
if (hasmissle == true && Input.GetKeyDown(KeyCode.F))
{
misslelin();
}
float ForwardInput = Input.GetAxis("Vertical");
PlayerRb.AddForce(FocalPoint.transform.forward * ForwardInput * speed);
PowerUpIndicator.transform.position = transform.position + new Vector3(0, -0.5f, 0);
misslewarner.transform.position = transform.position + new Vector3(0, -0.5f, 0);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("powerup"))
{
HasPowerup = true;
PowerUpIndicator.gameObject.SetActive(true);
StartCoroutine(PowerUpCountdownRoutine());
Destroy(other.gameObject);
}
if (other.CompareTag("missle"))
{
hasmissle = true;
misslewarner.gameObject.SetActive(true);
Destroy(other.gameObject);
StartCoroutine(PowerUpCountdownRoutine2());
}
}
IEnumerator PowerUpCountdownRoutine2()
{
yield return new WaitForSeconds(7);
hasmissle = false;
misslewarner.gameObject.SetActive(false);
}
IEnumerator PowerUpCountdownRoutine()
{
yield return new WaitForSeconds(7);
HasPowerup = false;
PowerUpIndicator.gameObject.SetActive(false);
}
void misslelin()
{
foreach (var enemy in FindObjectsOfType<Enemy>())
{
tmpmislle = Instantiate(missle, transform.position + Vector3.up, Quaternion.identity);
rocketBehavior.Fire(enemy.transform);
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Enemy") && HasPowerup)
{
Rigidbody EnemyRigid = collision.gameObject.GetComponent<Rigidbody>();
Vector3 awayfromplayer = collision.gameObject.transform.position - transform.position;
EnemyRigid.AddForce(awayfromplayer * PowerUpStrenght, ForceMode.Impulse);
}
}
}
and this is my missle script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RocketBehavior : MonoBehaviour
{
private Transform target;
private float speed = 15.0f;
private bool homing;
private float rocketStrength = 15.0f;
private float aliveTimer = 5.0f;
void Update()
{
if (homing && target != null)
{
Vector3 moveDirection = (target.transform.position -
transform.position).normalized;
transform.position += moveDirection * speed * Time.deltaTime;
transform.LookAt(target);
}
}
void OnCollisionEnter(Collision col)
{
if (target != null)
{
if (col.gameObject.CompareTag(target.tag))
{
Rigidbody targetRigidbody = col.gameObject.GetComponent<Rigidbody>();
Vector3 away = col.transform.position - transform.position;
away.y = 0;
targetRigidbody.AddForce(away.normalized * rocketStrength, ForceMode.Impulse);
Destroy(gameObject);
}
}
}
public void Fire(Transform homingTarget)
{
target = homingTarget;
homing = true;
Destroy(this.gameObject, aliveTimer);
}
}
this is my player object:
and this is my missle prefab: