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 Der_Kevin · Dec 28, 2015 at 11:51 AM · transformarray

fill public Transform[] on Awake?

hey! i want to fill in some Target Transform Dynamically

 public Transform[] m_Targets;                     // All the targets the camera needs to encompass.

so it should look on Awake for all Spawn Points available and fill it in the array. I did it this way:

 private void Awake ()
         {
             m_Targets[1] = GameObject.Find("Spawn1");
             m_Targets[2] = GameObject.Find("Spawn2");
             m_Targets[3] = GameObject.Find("Spawn3");
             m_Targets[4] = GameObject.Find("Spawn4");
         }

so i expected that on startup all Spawn1-4 GameObjects gets placed in the Transform Targets field but nothing happens on startup and the fileds stay empty. could you tell me why?

Comment
Add comment · Show 2
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 saschandroid · Dec 28, 2015 at 12:37 PM 0
Share

Are you sure you don't get any errors and the code compiles? If I try to assign a GameObject (returned by GameObject.Find()) to a Transform my code doesn't compile.

avatar image 12boulla · Dec 28, 2015 at 12:40 PM 0
Share

I have not tested this but shouldn't tyou put a .transform at the end of the 4 lines?:

 private void Awake ()
          {
              m_Targets[1] = GameObject.Find("Spawn1").transform;
              m_Targets[2] = GameObject.Find("Spawn2").transform;
              m_Targets[3] = GameObject.Find("Spawn3").transform;
              m_Targets[4] = GameObject.Find("Spawn4").transform;
          }

1 Reply

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

Answer by Priyanshu · Dec 28, 2015 at 02:41 PM

  • You need to fill transform into Transform[] array as @12boulla suggested. Since GameObject.Find returns GameObject type.

      m_Targets[0] = GameObject.Find("Spawn1").transform;
    
    
  • You also need to specify array size inside Awake()

      m_Targets = new Transform[number of items]; // In your case 4
     m_Targets[0] = GameObject.Find("Spawn1").transform;
    
    
  • Array cells start from 0 instead of 1. If u start with m_Targets[1], u will waste 0th cell.

    m_Targets[0] = GameObject.Find("Spawn1").transform;

  • If you don't have prior knowledge of, how many objects u want to save. You might need List.

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 Der_Kevin · Dec 28, 2015 at 03:36 PM 0
Share

Hey! Thanks! The lists are for when i dont know the size of the [number of items] right?

avatar image Priyanshu · Dec 28, 2015 at 03:38 PM 0
Share

Yes. And arrays are faster than List. So always try to give them precedence. Gud Luck :)

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

35 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

Related Questions

Transform[] Array - fill it from another Script 1 Answer

Drag and Drop. Grid Array to save if something is placed not working. Fix?,Drag and Drop Grid array to save the positions of placed Object not working. Fix? 1 Answer

IndexOutOfRangeException error when getting transform of array object 0 Answers

Array problem, I'm novice 1 Answer

Object reference not set to an instance of an object + array of positions 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