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 /
This question was closed May 23, 2019 at 12:23 PM by adovehv for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by adovehv · May 23, 2019 at 09:56 AM · instantiateprefabplayerinstantiate prefabenemy spawn

Problem when assigning a prefab

Hi! I have a enemy "dad" that instantiates other enemy "child" in a specific point, my problem is... i assign the "player" prefab to target variable in the enemy that is going to be instantiated, but this enemy prefab do target to the original position of the player prefab was create, (for example, target to "0,2.5,0"). I have another enemy that is not instantiated, is from the beginning in the scene and work fine, where is the problem? Thanks :)

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

  • Sort: 
avatar image
0
Best Answer

Answer by tormentoarmagedoom · May 23, 2019 at 10:19 AM

Hello.

Can you post the code you are using?

You say " i assign the "player" prefab to target variable" but.. a prefab is not a real object in the scene, as it name says is pre-fabricated, is not real yet, is not instantiated in the scene. You need to assign as target to follow a real instantiated object in the scene (you can find it, assign via script, assign via inspector... som any ways, but a created object!)

As said, please postr your code to see what are you trying to do!

Bye!

Comment
Add comment · Show 4 · 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
avatar image adovehv · May 23, 2019 at 10:28 AM 0
Share
  void CheckDistance()
     {
         if (Vector3.Distance(target.position,
             transform.position) <= chaseRadius
             && Vector3.Distance(target.position, transform.position) > attackRadius)
         {
             if (currentState == EnemyState.idle || currentState == EnemyState.walk
                 && currentState != EnemyState.stagger)
             {
 
                 Vector3 temp = Vector3.$$anonymous$$oveTowards(transform.position, target.position,
                 moveSpeed * Time.deltaTime);
                                  
                 print("APARECE");
                 anim.SetBool("move", true);
                 rb2d.$$anonymous$$ovePosition(temp);
                 ChangeState(EnemyState.walk);                
             }
         }
     }

 using UnityEngine;
 using System.Collections.Generic;
 using PolyNav;
 
 //example
 [RequireComponent(typeof(PolyNavAgent))]
 public class FollowTarget : $$anonymous$$onoBehaviour{
 
     public Transform target; 
     
     private PolyNavAgent _agent;
     private PolyNavAgent agent{
         get {return _agent != null? _agent : _agent = GetComponent<PolyNavAgent>();}
     }
 
     void Update() {
         if (target != null){
             agent.SetDestination(target.position);
         }
     }
    
 }
avatar image adovehv adovehv · May 23, 2019 at 11:01 AM 0
Share

I imagine that there has to be some way of assigning the "player" that is in the scene, to the variable target, without dragging it, in the void start or awake or something like this.

avatar image tormentoarmagedoom adovehv · May 23, 2019 at 11:58 AM 1
Share

Yes.

Best way is assing a tag to the player (for example "Player"). SElect the player, and in inspector top bar, choose a tag or create a newone adn assign it.

Then to find the player in the scene use this:

GameObject PlayerObject = GameObject.FindObjectWithTag("Player");

So now, PlayerObject variable is the Player in the scene.

Bye!

Show more comments

Follow this Question

Answers Answers and Comments

165 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 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

spawn from pool only if pool has inactive gameobjects 3 Answers

How do I rotate a prefab and instantiate it to go forward of my player? 0 Answers

How to change Image on button click in a prefab that is instantiated 1 Answer

Multiplayer prefab player fall through floor on instantiate 0 Answers

Instantiating random prefabs when the player passes through a trigger 1 Answer


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