• 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 /
avatar image
0
Question by Diamantiness · Aug 04, 2020 at 01:45 AM · prefabs

Im having an issue with my prefabs.

Hello everyone!

I have just started coding and using unity, and Im loving it! Solving issues has been very satisfying, until now. Now, im faced with something which makes me want to slam my computer so far down my throat i can use it by digesting it (i hope that is a good explanation of my rage). Im sure there is an easy fix to my issue, but as I said, im new so idk what that fix is.

The issue:

I have a prefab which is coded to follow the players location at all times. The issue is that, while it says that the target is the player, after i put the players prefab into it, the prefab ends up going to the players spawn location and thats it, while if i change the target of the prefab while it is in the scene with a player that is in the scene it will move with the player. This is an issue as I have a script to instantiate these prefabs into the level, so they all just move to the players spawn location, and it doesnt let me move my in-scene player into the prefabs target tansform when its just the prefab in my assets, not in the scene.

This is my first post, so i dont really know how to give more info, but i would love to give more info on the issue once i understand how to.

This is the code for my enemy:

 using UnityEngine;
 using Pathfinding;
  
 public class MeleeAI : MonoBehaviour
 {
  
     public Transform target;
  
     public float speed = 40f;
     public float nextWaypoint = 2f;
  
     Path path;
     int currentWaypoint = 0;
     bool reachedEndPath = false;
  
     Seeker seeker;
     Rigidbody2D rb;
  
     void Start()
     {
  
         seeker = GetComponent<Seeker>();
         rb = GetComponent<Rigidbody2D>();
  
         InvokeRepeating("UpdatePath", 0f, 0.1f);
  
     }
  
     void UpdatePath()
     {
  
         if (seeker.IsDone())
             seeker.StartPath(rb.position, target.position, OnPathComplete);
  
     }
  
     void OnPathComplete(Path p)
     {
  
         if (!p.error)
         {
  
             path = p;
  
             currentWaypoint = 0;
  
         }
  
     }
  
     void FixedUpdate()
     {
  
         if (path == null)
             return;
  
         if (currentWaypoint >= path.vectorPath.Count)
         {
  
             reachedEndPath = true;
             return;
  
         }
  
         else
         {
  
             reachedEndPath = false;
  
         }
  
         Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
         Vector2 force = direction * speed * Time.deltaTime;
  
         rb.AddForce(force * speed);
  
         float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
  
         if (distance < nextWaypoint)
         {
  
             currentWaypoint++;
  
         }
  
     }
 }
  
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
0

Answer by cwalshwarder · Aug 04, 2020 at 02:09 AM

It's probably going to the transform of the player prefab (gameobject in folders), which isn't the same as the prefab instance's transform (gameobject in hierarchy). To find the player in the scene, you could use a tag, or a script on the player with a singleton coding pattern.

Comment
Add comment · Show 2 · 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 Diamantiness · Aug 04, 2020 at 02:20 AM 0
Share

How could i make my code get the position of all objects tagged as "Player". (BTW its fine if u dont answer, i get that correcting code for some random online isnt something enjoyable)

avatar image cwalshwarder Diamantiness · Aug 04, 2020 at 04:17 PM 0
Share

Try to get familiar with unity's documentation. https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

139 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

Related Questions

How to make On Click() work with prefabs? 1 Answer

Changing the Material of an instance of a prefab only! 2 Answers

Trigger resets to 1-1-1 scale automatically. (Not supposed to) 0 Answers

Text Not Being Displayed above Instantiated Prefabs 0 Answers

Prefub Edit Collider buttons are not displayed on the Scene 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