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 Chris42 · Oct 10, 2012 at 05:38 AM · classpassing

Passing an object of a custom class in order to access its variables

Hello all,

I have a set of starships in any given level - new ones can arrive, they can be destroyed, the works. Each of them has a Targeting Controller component, a custom component that tracks other ships and decides whether they can be targeted or not. When a new ship enters the game, that ship gets added to all ships' Targeting Controllers, and when it dies, it is removed. This isn't a problem - all of this is good so far.

In order to reduce overhead and GetComponent calls, instead of the Targeting Controller containing a simple GameObject array of all the ships, it maintains a list of custom 'Target' objects. The Target class contains a bunch of variables containing references to components on each target ( all initialized when that target arrives), and critically, the distance to that target. Here's an example:

 // represents a target
 class Target {
     // the target's gameobject
     var obj:GameObject;
     // the target's transform
     var tform:Transform;
     // the target's rigidbody
     var rb:Rigidbody;
     // distance to the target
     var dist:float;
 }

I've abbreviated that for simplicity, there are a bunch more cached things in there. Critically, the Targeting Controller periodically (every 0.5 seconds for now) checks the distance between each Target and the ship and stores it in the dist variable. This is important because we do a lot of stuff with auras, shields, missiles with ranges and don't want to be doing Vector3.Distance() 50 times in a frame when we could do it once here and just retrieve it.

Again, so far so good. Each ship maintains its own List. in the Targeting Controller and updates the distances to other ships. My problem occurs in my various AI scripts, whom I pass a Target, say, from a function in the Targeting Controller called FindNearestTarget(), which returns myTarget, the Target object with a Transform that is closest.

At this point, I'd like to do a simple "when closer than x units, shoot" script ( if (myTarget.dist <= attackDistance) ). The problem is that the Target that has been passed from FindNearestTarget() no longer updates its distance once passed, it's stuck at the point at which FindNearestTarget() was called. I want to pass a... pointer, I think it's called? so that every time I look at myTarget, I see the value that is being updated by the Targeting Controller.

I can't seem to figure out how to do this. Help would be much appreciated, I feel like there is something simple that I'm missing.

Comment
Add comment · Show 3
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 whydoidoit · Oct 10, 2012 at 06:36 AM 0
Share

Hi Chris - there's got to be something else going on. Passing a variable which has a reference to something that is a class, not a struct, always passes a pointer - you don't need to do anything explicit - it's alway a reference back to the object you created.

avatar image whydoidoit · Oct 10, 2012 at 06:38 AM 0
Share

Are you perhaps reallocating these classes more than once? Hence breaking the reference to the previously passed object?

avatar image Chris42 · Oct 10, 2012 at 06:30 PM 0
Share

Alright, checked everything and feel stupid - there's a reallocation going on that shouldn't be. Thanks for the pointer (haha, pun not intended)

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Passing a class instance to a method 1 Answer

Attach a value to an instantiate object and call it in the class of this object 0 Answers

c# passing classes through functions 2 Answers

System.NullReferenceException 1 Answer

many "characters" data 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