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

A.I Chase problem [SOLVED]

Discussion in 'Scripting' started by WilibertXXIV, Feb 22, 2017.

  1. WilibertXXIV

    WilibertXXIV

    Joined:
    Feb 15, 2017
    Posts:
    5
    I found this code on the forum some time ago and it does just what I want but I do run into quite a problem that I would like to understand.

    It's quite simple; I created an enemy, added the script component to it, assigned my Player (to one from hierarchy) and everything worked like a charm. But then, after prefabbing the enemy and assigning my Player from prefab the Enemy is attracted by the default location of the Player.

    I just can't figure why that is.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Attract : MonoBehaviour
    5. {
    6.     public int speed;
    7.     public GameObject player;
    8.  
    9.     void Update()
    10.     {
    11.         Vector3 localPosition = player.transform.position - transform.position;
    12.         localPosition = localPosition.normalized; // The normalized direction in LOCAL space
    13.         transform.Translate(localPosition.x * Time.deltaTime * speed, localPosition.y * Time.deltaTime * speed, localPosition.z * Time.deltaTime * speed);
    14.     }
    15. }
    Edit: Oh and this pops to in the console..even though i can clearly see the Player assigned
    "UnassignedReferenceException: The variable player of Attract has not been assigned.
    You probably need to assign the player variable of the Attract script in the inspector.
    Attract.Update () (at Assets/Scripts/Attract.cs:12)"
     
  2. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    a prefab can't use a component in your scene, if you delete the player in your scene how did the prefab can know what player to use ?

    So you need to set another prefab as the player in editor (and so instanciate it in Start) or you can find your player in your scene in the start like :

    GameObject.FindWithTag("player"); or something like this
     
    WilibertXXIV likes this.
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Prefabs are their own unique object from a coding standpoint. When you instantiate an enemy he is a copy of Prefab (a new instance) and his variables are not assigned. You don't ever want to assign objects in the inspector to Prefabs. (its possible but it will lead to bugs because of what I said before). Instead you always need to dynamically assign these variables in the script:

    This assumes your player as a player Tag = "Player":
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Attract : MonoBehaviour
    4. {
    5.     public int speed;
    6.     public GameObject player;
    7.  
    8.     void Awake()
    9.      {
    10.              player = GameObject.FindWithTag("Player");
    11.       }
    12.     void Update()
    13.     {
    14.         Vector3 localPosition = player.transform.position - transform.position;
    15.         localPosition = localPosition.normalized; // The normalized direction in LOCAL space
    16.         transform.Translate(localPosition.x * Time.deltaTime * speed, localPosition.y * Time.deltaTime * speed, localPosition.z * Time.deltaTime * speed);
    17.     }
    18. }
     
    WilibertXXIV likes this.
  4. WilibertXXIV

    WilibertXXIV

    Joined:
    Feb 15, 2017
    Posts:
    5
    Thanks to you both, it works! Thanks for the additional information!
     
    CrymX likes this.