N
N
NoobNoob22021-08-08 13:54:48
C++ / C#
NoobNoob2, 2021-08-08 13:54:48

Scripts don't work, what's the problem?

I wrote a code that makes the tower shoot at enemies, (I followed the guide if I did anything), I checked for 30 minutes what was wrong, I wrote everything as it should be, but it still didn’t work. And yes, I assigned all scripts to prefabs.
Here is the guide: https://www.youtube.com/watch?v=AzKgQPocbes&t=1636s
Here is the bullet code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bulletTower : MonoBehaviour
{

    public float Speed;

    public Transform target;

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    { 
      transform.position = Vector3.MoveTowards(transform.position,target.position,Time.deltaTime*Speed);
        
    }
}

Here is the code of the tower and also the area of ​​​​action:
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;
    }

}

sing System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TowerTrigger : MonoBehaviour
{


    public Tower twr;

    public bool lockE;

    public GameObject curTarget;

    void OnTriggerEnter(Collider other)
    {

        if (other.CompareTag("enemyCube") && !lockE)
        {
            twr.target = other.gameObject.transform;
            curTarget = other.gameObject;
            lockE = true;
        }

    }

    void Update()
    {
        if (!curTarget)

        lockE = false;
    } 
    void OnTriggerExit(Collider other)
        {
         
            if (other.CompareTag("enemyCube") && other.gameObject == curTarget) 
           
            lockE = false;

           
       }       
              
            
}

These are all codes, now there will be screenshots of prefabs.
610fb7938872e190436404.png

610fb79bde99a375684035.png

610fb7a3ae566825428963.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loli E1ON, 2021-08-08
@NoobNoob2

Poorly rewritten)) Are there too many enters?

if (target)

LookAtObj.transform.LookAt(target);

if (!IsShoot)

StartCoroutine(shoot());

The author of the lesson posted the source code on google drive in the description of the video lesson.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question