How to drag an empty into a prefab
hey guys, I'm new to unity and iI'm trying to make my own game. After + 50hours coding on it, I'm stuck. In fact i created a prefab called "mob" and i want it to move to an empty which is randomly moving in an area all the 2 seconds. Anyways, i created my prefab and now i can't drag my empty in my prefab but I can if my IA isn't a prefab and i spawn it my by myself.
here is my IA code and some screenshot to explain you in detail:
http://www.noelshack.com/2018-43-7-1540700627-drag.png
http://www.noelshack.com/2018-43-7-1540700627-prefab.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootIA : MonoBehaviour {
public float speed;
public float stoppingDistance;
public float retreatDistance;
public float DetectionDistance;
private float waitTime;
public float startWaitTime;
public Transform moveSpot;
public float minX;
public float maxX;
public float minY;
public float maxY;
public GameObject projectile;
private Transform player;
private bool DepAutorise = true;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player").transform;
waitTime = startWaitTime;
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
}
// Update is called once per frame
void Update () {
//print(transform.position);
if (transform.position.x > minX && transform.position.x < maxX && transform.position.y > minY && transform.position.y < maxY && DepAutorise )
{
if ((Vector2.Distance(transform.position, player.position) > stoppingDistance) && (Vector2.Distance(transform.position, player.position) < DetectionDistance))
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
{
transform.position = this.transform.position;
}
else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
}
if (waitTime <= 0 && (Vector2.Distance(transform.position, player.position) < stoppingDistance))
{
Instantiate(projectile, transform.position, Quaternion.identity);
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
else {
transform.position = Vector2.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime);
waitTime = startWaitTime;
DepAutorise = false;
if (Vector2.Distance(transform.position, moveSpot.position) < 0.02f)
{
DepAutorise = true;
}
}
}
}
my spawner :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public float rotationSpeed;
private float waitTime;
public float startWaitTime;
public GameObject mob;
// Use this for initialization
void Start () {
waitTime = startWaitTime;
}
// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
if (waitTime <= 0)
{
Instantiate(mob, transform.position, Quaternion.identity);
waitTime = startWaitTime;
}
if (waitTime >= 0)
{
waitTime -= Time.deltaTime;
}
}
}
Maybe you have a solution or a way to not use an empty, but I'm seriously stuck. Thanks in advance, Naexys.
Answer by v-ercart · Nov 15, 2018 at 10:49 PM
Things that are in scenes can't be referenced in a prefab. If you create an instance of that prefab in your scene, that prefab instance (which is in the scene itself) can reference something else in that scene.
Alternatively, a prefab can reference another prefab.
If you're spawning the prefab via Instantiate(), then set the MovePoint right after you instantiate it. Use Find() or something to get a reference to the transform in your scene, there are several good options to get a reference.
Your answer
![](https://koobas.hobune.stream/wayback/20220612181101im_/https://answers.unity.com/themes/thub/images/avi.jpg)