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
1
Question by David 25 · Apr 06, 2011 at 05:39 AM · gameobjectarraybce0019bce0017

Use an objects (from array) position to focus a camera on

I have a list of buttons which is created by reading all the objects in an array. This works. But now I want to move the camera target to the transform.position of the object that is related to the clicked button. So if the label of the button says "ObjectX", I want to move the camera target to ObjectX.transform.position.

've been working with gameObject.Find and Transform.Find but I can not get it to work. The code below generates a nullReferenceException. I think I'm doing something wrong with gameObject.Find(toString).transform.position. Basically I need to find the object named as stored in toString (or CreateObjectList01.listOfObjects[i]) (I tried both into the gameObject.Find() but it didn't work) and get it's position coordinates which is a Vector3 I presume.

for (var i = 0; i < CreateObjectList01.listOfObjects.length; i++) { var toString = "" + CreateObjectList01.listOfObjects[i]; //converts array objects to type string

if (GUILayout.Button(toString))
{
gameObject.Find("Cameratarget").transform.position = gameObject.Find(toString).transform.position; } }

This compiles, but gives me the null reference exception when I click on one of the buttons. Your first suggestion doesn't solve it, the nullreference remains. The second suggestion triggers the following error: "Assets/myGUI.js(84,114): BCE0019: 'transform' is not a member of 'Object'. " I assume that means it's an array of Objects which is not the same as GameObjects. I did try that before therefore I tried to use the GameObject.Find instead. Is there perhaps a way to convert an Object to a GameObject?

Cameratarget exists and toString is not a GameObject but a string. If I try

 var toString = CreateObjectList01.listOfObjects[i];  //object type

instead of

 var toString = "" + CreateObjectList01.listOfObjects[i];  //string type

I get an error : BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(Object)'.

Any help would be greatly appreciated!

Edit: The string conversion is done (together with string split) to get the object names in the button-labels without (unityengine.gameobject) in them. I tried both the "toString" as a string and object (like mentioned above. And also tried "CreateObjectList01.listOfObjects", neither worked, I think I'm doing very simple completely wrong, just can figure out what.

Comment
Add comment · Show 1
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 KeithK · Apr 06, 2011 at 07:00 AM 0
Share

Please specify more clearly what the previous suggestions actually were. Or else you may just end up getting them again.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Apr 06, 2011 at 07:03 AM

There is also blahblah.ToString() (ignoreing blahblah) which converts almost anything to a string, but don't you want object.name? Try CreateObjectList01.listOfObjects[i].name

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 KeithK · Apr 06, 2011 at 07:14 AM 0
Share

A null reference means that the Find() function is not finding any GameObjects with their "name" property set to whatever you are storing in toString. Use print() functions to check if stored values are what you expect them to be.

avatar image
0

Answer by David 25 · Apr 06, 2011 at 07:04 PM

DaveA: I've implemented the ToString to create the string which works great! With the nullexception I have less luck I tried the following:

GameObject.Find("Cameratarget").transform.position = GameObject.Find(CreateObjectList01.listOfObjects[i]).transform.position;' this results in BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(Object)'.

Than I tried to get the name into a variable to use it later

var tazer = CreateObjectList01.listOfObjects[i].name;

But this results in: BCE0019:'name' is not a member of 'Object'.

I also tried:

var tazer = CreateObjectList01.listOfObjects[i];
GameObject.Find("Cameratarget").transform.position = GameObject.Find(tazer).transform.position; 

Which results in BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(Object)'.

Do you have any clue? It seems to me that the array contains Objects and I need GameObjects, when I print them I get "objectname (UnityEngine.Transform)".

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
avatar image
0

Answer by David 25 · Apr 06, 2011 at 10:23 PM

I just solved it: the names in toString contained a space at the end after the string splitting, but the object names do not end with a space. Therefore the objects could not be found. Thanks for your help!

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

No one has followed this question yet.

Related Questions

Getting an error while script reference says it's ok 1 Answer

Transport unknown amout of objects with GameObject array 0 Answers

View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers

Access a String array in one script from another script 0 Answers

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 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