- Home /
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.
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.
Are you perhaps reallocating these classes more than once? Hence breaking the reference to the previously passed object?
Alright, checked everything and feel stupid - there's a reallocation going on that shouldn't be. Thanks for the pointer (haha, pun not intended)
Your answer
Follow this Question
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