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 /
avatar image
0
Question by LotusElise · May 02, 2019 at 09:34 AM · instantiateassign

Instantiate multiple objects that follow the FirstPersonController

I'm trying to make a multitude of instantiated objects that will follow my FirstPersonController around when I walk. This FristPersonController has a capsule object as a child, so the character has some visibility.

For this, I have a gameBrain with the following script assigned to it:

 public class gameBrain : MonoBehaviour {
 
     void Start () {
         GameObject prefab = Resources.Load ("EthanScript") as GameObject;
 
         for (int i = 0; i < 100; i++) {
             GameObject go = Instantiate (prefab) as GameObject; 
             go.transform.position = new Vector3 (i*0.10f, 0, i*2); 
         }
     }
 }

The object EthanScript has a script attached to it that makes it follow any object you assign to it.

 public class LootAtFollow : MonoBehaviour {
 
     public Transform myTarget; 
 
     public float mySpeed = 10.0f; 
     public float epsilon = 0.1f; 
     
     void Update () {
         transform.LookAt (myTarget.position); 
 
         if ((transform.position - myTarget.position).magnitude > epsilon) {
             transform.Translate (0.0f, 0.0f, mySpeed * Time.deltaTime); 
         }
     }
 }

Now I want to immediately assign the capsule that is my controller to the instantiated objects. How do I access the capsule (child of Controller) in code and assign it to be the myTarget? I would like this to be done in code, because I don't want to have to drag and drop the capsule a hundred times into the myTarget slot in the Inspector.

I tried multiple things but keep running into multiple errors. I hope someone has a tip for me! Thanks in advance!

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 tormentoarmagedoom · May 02, 2019 at 11:39 AM

Hello!

Yo uare very close! YOu need to do this (you can assign the variable directly from GameBrain right after instantiate! You need to use the function transform.find to get the child (lets say the controller Object is called "FPCObject" and the capsule child is called PersonCapsule)

 GameObject go = Instantiate (prefab, new Vector3 (i*0.10f, 0, i*2), quaternion.identity ); 
 go.GetComponent<LootAtFollow>().myTarget = FPCObject.transform.Find("PersonCapsule");

This will assign the PersonCapsule transform to the myTarget variable right after the prefab is instantiated. You will see that the variable is asigned automatikly before the Start() of LootAtFollow script.

Note FPCObject variable is a GameObject variable, the First person controller gameobject.

Bye!

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 LotusElise · May 02, 2019 at 12:00 PM 1
Share

Thank you! This works!

avatar image tormentoarmagedoom LotusElise · May 02, 2019 at 01:42 PM 0
Share

You're welcome!

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

126 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

Related Questions

Instantiate and then assign? 1 Answer

How to force a variable to prefab 1 Answer

Checking if object intersects? 1 Answer

Variable not Assigned 1 Answer

While Loop Not Assigning Array 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