• 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 e-bonneville · Jan 25, 2011 at 05:28 PM · reference

How do I pass a reference?

If I'm accessing a GameObject via its Transform component, can I pass a reference to that component to a function and have the function access that GameObject?


Here's my script:

if (mapData.ContainsKey(xpos)) {
    neighbors[0] = (Transform)mapData[xpos];
}

Where neighbors[] is an array of Transforms. I'm trying to get transforms from the above hash into an array.

I'm trying to create a voxel terrain of cubes that each know what their neighbors are. Each cube originally knew its neighbors through raycasting, but that was to slow. So I tried another technique. When I generated the terrain, I created a hash of all the transforms, and gave that hash to each cube.

My function runs through the hash to see if it has anything near it in the "X Positive", that is, to the right, and if there is, sets neighbors[0] to equal the transform of that gameObject. It's obviously not working at the moment, but I have no idea why.

Comment
Add comment · Show 4
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 Jessy · Jan 25, 2011 at 07:27 PM 0
Share

Why are you not using a Dictionary?

avatar image e-bonneville · Jan 25, 2011 at 07:46 PM 0
Share

Because I don't know how they work. I only just now started to do any serious coding (Anything previous to this was just for fun) and never had to. Plus, I only just started using C# a couple days ago, so I'm not completely familiar with the API. How do they (Dictionaries) work?

avatar image Jessy · Jan 25, 2011 at 08:11 PM 0
Share

It's very similar to a HashTable, only better. Google it, try it out, and see if you're still having problems after you introduce type safety into the equation.

avatar image e-bonneville · Jan 25, 2011 at 09:57 PM 0
Share

Will do. Thanks for the help.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessy · Jan 25, 2011 at 05:30 PM

Yes. Transform is a class. You'd just do

thatTransform.gameObject

http://unity3d.com/support/documentation/ScriptReference/Component-gameObject.html

Comment
Add comment · Show 6 · 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 e-bonneville · Jan 25, 2011 at 06:10 PM 0
Share

Hmm... So I'm trying to store a reference to a gameObject in a hash and then reference it like so: mapData[xneg].gameObject But it still won't work. What am I doing wrong?

avatar image DaveA · Jan 25, 2011 at 06:16 PM 0
Share

can you post code?

avatar image Jessy · Jan 25, 2011 at 06:24 PM 0
Share

Why did you say you're storing a Transform then? Yes, please post code; I no longer have any idea what you're saying. And apparently the thing that is said above is not the thing that you think it is, to me. ;-)

avatar image e-bonneville · Jan 25, 2011 at 06:37 PM 0
Share

You're quite right, I was being very... special. $$anonymous$$y apologies, I'll post code. :)

avatar image DaveA · Jan 25, 2011 at 09:03 PM 0
Share

What about where you stuff the values into the table?

Show more comments
avatar image
0

Answer by DaveA · Jan 25, 2011 at 06:19 PM

If you are assigning mapDatg[xneg] = aTransform; it should work, because you access mapData[xneg] as a transform, so that with .gameObject should pull that out. If, on the other hand, you are assiging mapData[xneg] = aGameObject, then there is no GameObject.gameObject. Some code to look at might help.

Comment
Add comment · Show 4 · 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 e-bonneville · Jan 25, 2011 at 06:37 PM 0
Share

Ah. Hmm, let me try that.

avatar image e-bonneville · Jan 25, 2011 at 07:03 PM 0
Share

Wait, don't you mean aTransform = mapData[xneg]?

avatar image DaveA · Jan 25, 2011 at 09:04 PM 0
Share

I mean where you stuff the values into the table, what are you storing? Because if it's a Transform, it should work. If it's a GameObject, it won't. FWIW I've used Hashtables in this way with no problem, so I know we can get you fixed up.

avatar image e-bonneville · Jan 25, 2011 at 09:58 PM 0
Share

Ah, I see. Yeah, I'm storing transforms in there, that part is working fine.

Cool!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Getting a reference to a group of ScriptableObjects implemented at runtime? 0 Answers

Folder Trick Doesn't Work: Referencing JS from C# 0 Answers

Add external DLL reference 1 Answer

What is the Additive Reference Pose checkbox for? 1 Answer

How do I reference the cameraEye in another script 0 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