Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 diogm · May 21, 2014 at 07:34 PM · positionobjectactivation

GameObject.find and positioning

Hello, I am instantiating objects at run-time and i'm naming them i+"empty" , where i is the variable for the cycle that is instantiating the objects, and when I try to find those objects in another cycle and set them active like this:

 for (int i = 0; i<cardList.Count; i++) {
       GameObject emp;
       emp = GameObject.Find(i+"empty") as GameObject;
       emp.SetActive(true);
   }

When I run the project I get a "Object reference not set to an instance of an object." error, what am I doing wrong?

Another question, take the same object as above, how can I position hit so it gets the same position across the various sizes of monitors/resolutions ? I'm actually positioning the objects with their pixel inset components but I want to position them with the transform component.

Thanks in advance

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 Jeff-Kesselman · May 21, 2014 at 07:40 PM 0
Share

We need a lot more information. Please give us the following:

The entire error message as presented by Unity, not your paraphrase thereof.

A screen shot of the hierarchy window at the time this code attempts to run.

avatar image diogm · May 21, 2014 at 07:59 PM 0
Share

Problem was that I was tryinh to find an inactive object... I still need help with the positioning though

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · May 21, 2014 at 07:41 PM

GameObject.Find only finds active GameObjects, unfortunately.

Remember, though, that Instantiate returns a reference to the newly created object:

 //assumes "empPrefab" is set elsewhere
 GameObject emp = Instantiate(empPrefab) as GameObject;

You could keep that reference, perhaps in an array, list, dictionary, or some other collection. See the Unity wiki article Which Kind Of Array Or Collection Should I Use?

Comment
Add comment · Show 1 · 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 diogm · May 21, 2014 at 07:54 PM 0
Share

Guess the above answer says all, thanks! Is tagging the objects the only way to .Find it?

And I still need some help with the positioning issue, please.

Thanks again for the fast answers!

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

22 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

Related Questions

Having an object point towards mouse position in 3d world 1 Answer

Check if an object is changing position C# 3 Answers

set child object position when playing the animation 0 Answers

How to move an object from one position to desired position? 0 Answers

Return object to position 3 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