Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Naexys24 · Oct 28, 2018 at 04:14 PM · prefabempty

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Car permeates through the terrain 1 Answer

Newly Instantiated Prefabs Not Changing Colour 1 Answer

Event called on prefab instantiate 1 Answer

Destroy prefabs after they have been instantiated (C#) 1 Answer

Instantiated Object Has Wrong Rotation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges