Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unable to assign public Gameobject from instuctor or script

Discussion in '2D' started by Waleed Khan, Aug 6, 2015.

  1. Waleed Khan

    Waleed Khan

    Joined:
    Aug 1, 2015
    Posts:
    2
    How to use brackeys ENEMY AI 2d Platformer tutorial script modifed as prefab.
    When i make Enemy a prefab it's "public Transformer target" is unable to assign from instructor,
    when i do it in script dont work target = GameObject.FindGameObjectWithTag("car").transform.
    Need Help
     
  2. Waleed Khan

    Waleed Khan

    Joined:
    Aug 1, 2015
    Posts:
    2
    using UnityEngine;
    using System.Collections;
    using Pathfinding;


    [RequireComponent (typeof(Seeker))]
    [RequireComponent (typeof(Rigidbody2D))]

    public class EnemyAI : MonoBehaviour {

    // Use this for initialization

    //what to chase?
    public Transform target;
    private GameObject c;
    //each sec update path
    public float updateRate = 2f;

    // catching
    private Seeker seeker;
    private Rigidbody2D rb;

    //The calculated path
    public Path path;

    //AI speed
    public float speed = 300f;
    public ForceMode2D fMode;

    [HideInInspector]
    public bool pathIsEnded = false;

    //Max distance from AI
    public float nextWaypointDistance = 3;

    //waypoint we are moving towards
    private int currentWaypoint = 0;
    private bool searchingForPlayer = false;
    private float maxSqrDistance = 16; //4 meters
    private float Range = 100;
    private GameObject car;

    void Start () {

    c = GameObject.FindGameObjectWithTag("car"); //problem occurs here
    target = c.transform; //target remains null
    seeker = GetComponent<Seeker> ();
    rb = GetComponent<Rigidbody2D> ();

    if (target == null) {
    //Debug.LogError("No player");
    if(!searchingForPlayer)
    {
    searchingForPlayer = true;
    StartCoroutine(SearchForPlayer());
    }
    return;

    }

    //strat new path
    seeker.StartPath (transform.position,target.position,OnPathComplete);
    StartCoroutine (UpdatePath ());

    }

    void Awake()
    {
    target = GameObject.FindGameObjectWithTag ("car").transform;
    if (target == null) {
    //Debug.LogError("No player");
    if(!searchingForPlayer)
    {
    searchingForPlayer = true;
    StartCoroutine(SearchForPlayer());
    }
    return;

    }
    }

    IEnumerator SearchForPlayer()
    {
    GameObject sResult = GameObject.FindGameObjectWithTag("car");
    if(sResult == null)
    {
    yield return new WaitForSeconds(0.5f);
    StartCoroutine(SearchForPlayer());
    }
    else{
    target = sResult.transform;
    StartCoroutine(UpdatePath());
    searchingForPlayer = false;


    }
    }

    IEnumerator UpdatePath()
    {
    if (target == null) {
    //Debug.LogError("No player");
    if(!searchingForPlayer)
    {
    searchingForPlayer = true;
    StartCoroutine(SearchForPlayer());
    }
    return false;

    }

    //start new path
    seeker.StartPath (transform.position,target.position,OnPathComplete);
    yield return new WaitForSeconds (1f / updateRate);
    StartCoroutine (UpdatePath ());
    }

    public void OnPathComplete(Path p)
    {
    Debug.Log ("path error" + p.error);
    if (!p.error) {
    path = p;
    currentWaypoint = 0;
    }

    }

    void OnTriggerEnter2D(Collider2D collider)
    {

    Debug.Log("trigger");
    if (collider.gameObject.name == "car")
    {

    car = GameObject.FindGameObjectWithTag("car");
    Destroy(car);



    }

    }

    // Update is called once per frame
    void FixedUpdate () {

    //Range = Vector2.Distance (target.position, rb.transform.position);
    Range = 14;
    //Debug.Log (Range);

    if (target == null) {
    //Debug.LogError("No player");
    if(!searchingForPlayer)
    {
    searchingForPlayer = true;
    StartCoroutine(SearchForPlayer());
    }
    return;

    }
    if (path == null) {
    return;
    }

    if (currentWaypoint >= path.vectorPath.Count) {


    if(pathIsEnded)
    return;

    Debug.Log("End of Path");
    pathIsEnded = true;
    return;
    }
    pathIsEnded = false;

    //dir of next waypoint
    Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
    dir *= speed * Time.fixedDeltaTime;

    //move AI
    rb.AddForce (dir, fMode);

    float dist = Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]);
    if (dist < nextWaypointDistance && Range < maxSqrDistance) {
    currentWaypoint++;
    return;
    }



    }
    }
     
  3. BlueSin

    BlueSin

    Joined:
    Apr 26, 2013
    Posts:
    136
    You may have better luck getting an answer to this on the Brackeys Forums at: http://forum.brackeys.com/. Almost nobody is going to watch the video series and search through them just to answer your question, and the chances that somebody has seen the video already and is going to pop in here and help you are slim.