Answer the question
In order to leave comments, you need to log in
The variable target of Tower has not been assigned. What to do?
I wrote the code so that the tower would shoot and then this:
here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tower : MonoBehaviour
{
public Transform shootElement;
public Transform LookAtObj;
public float dmg = 5;
public float shootSpeed;
public GameObject bullet;
public Transform target;
public float shootDelay;
bool IsShoot;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (target)
LookAtObj.transform.LookAt(target);
if (!IsShoot)
StartCoroutine(shoot());
}
IEnumerator shoot()
{
IsShoot = true;
yield return new WaitForSeconds(shootDelay);
GameObject b = GameObject.Instantiate(bullet, shootElement.position, Quaternion.identity) as GameObject;
b.GetComponent<bulletTower>().target = target;
IsShoot = false;
}
}
Answer the question
In order to leave comments, you need to log in
the target is not assigned, but it is trying to shoot.
b.GetComponent<bulletTower>().target = target;
the problem is in this line, the Bullet Tower component is taken from a certain bullet and in its target field it tries to be assigned to the target from the Tower, judging by the error, which is not assigned
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question