• 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 Sorin_Fayte · 13 hours ago · objects

Failure to pass in a game object into an instatiated game object

Hello! I am having an issue where I am making an agar.io-esque game where the player's sprite follows the mouse cursor. The enemies are spawned at random locations and seek after the player. So far, player movement and enemy spawning is working fine. The issue I think is something to do with not being able to pass in the player sprite object into the script for "chasing" it.

I have the follow code for spawning the enemies.

public class Spawner : MonoBehaviour { public GameObject enemy; [SerializeField] private float Speed = 2f;

 void Start()
 {
     InvokeRepeating("Generate_Enemy", 0, Speed);
 }
 void Generate_Enemy()
 {
     int x = Random.Range(0, 900);
     int y = Random.Range(0, 500);
     Vector2 Target = Camera.main.ScreenToWorldPoint(new Vector2(x, y));

     Instantiate(enemy, Target, Quaternion.identity);
  }

}

The spawning works fine. The issue comes in when I try to make the enemies attracted to the player's sprite. They all seem to be attracted to the starting point. I have attempted to update the player position every tick, but it doesn't seem to work.

Here is my "chase" class

public class Chase : MonoBehaviour { [SerializeField] private GameObject thePlayer; [SerializeField] private float speed = 1.5f;

 void Update()
 {
     float step = speed * Time.deltaTime;
     transform.position = Vector2.MoveTowards(transform.position, thePlayer.transform.position, step);
 }

}

I think the problem lies in the unity interface. When I go to drag the player object into the slot for the enemy prefab, it doesn't let me, despite it being designated as a game object and it allows all other game objects to fill the slot. It just gives a red circle with a line on it.

When I make an enemy object at the start, it works fine and takes in the player object no problem. But when the spawner script instatiates a new one, it forgets how to find the player again. Thoughts?

I am not a complete newb to Unity or C#, but it may be something simple. Please help

Comment
Add comment · Show 7
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 dcmrobin · 13 hours ago 0
Share

Do you mind sharing some pictures of your problem?

avatar image dcmrobin dcmrobin · 13 hours ago 0
Share

@Sorin_Fayte

avatar image Sorin_Fayte dcmrobin · 9 hours ago 0
Share

alt text

I tried to get a picture together, but I find it hard to capture movement (I am new to posting on help sites...)

question-1.jpg (146.6 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sorin_Fayte · 5 hours ago

I figured out how to do it. I needed to avoid passing the object in via the Unity interface and bypass that entirely using the FindObjectByType() method. I made a new class "Player" and stored its x and y positions in their own variables that then updated their values every update tick. then, when I needed those values for the enemy movement and pointing toward the player, I referenced the values being updated in Player, not the vector2 values of the player object. Now it works perfectly. Just had to lean on my OO programming knowledge and not my Unity knowledge. Thank you @dcmrobin for trying to help. I really appreciate it!

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

187 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 should i go about having an object have 2 clickable areas? 2 Answers

UnityScript nested objects best practice 1 Answer

How do i make object when collided be cutted/culled? 0 Answers

How can I move two object next to each other while keeping their positions pixel perfect? 0 Answers

[Absolute beginner] Play animation on trigger (trigger = several fallen objects) script 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