- Home /
Not appropriate for UnityAnswers, asker will rephrase.
Game Object referencing from another script
Hello,
Say I have a layer of game objects and when a raycast hits 1 of the GO's, a script (RaycastScript) finds it by it's collider and then another script (OtherScript) tells it what to do until it's finished it's job. The GO can work again if a raycast hits it again.
Would it best to:
Make a variable to hold the GO in the OtherScript since it will be referenced multiple times throughout the script until it's finished it's job??
( I'm curious of how to tell the OtherScript that when the GO is finished it's purpose, it doesn't need to take up memory at the moment. So the variable is just on stand by until another object is hit. Will it make a new instance of the variable every time a new object is referenced? )
Don't make a variable and reference it every time as follows? RaycastScript.hitGameObject I believe that this is bad for processing, but what about having too many variables, is that bad for processing?
Create another script that controls the GO and is also attached to the GO?
Do some other thing that I haven't thought about?
Also, if I wanted to reference say: RaycastScript.hitGameObject.Layer multiple times in my OtherScript, is it also better to have a variable for that?
Sorry if this is 101 stuff. I just started out and I would greatly appreciate the opinion of somebody with experience. Trying to do what's best for processing speed and memory.
Thanks in advance
I've read through you question several times, and am confused by aspects of it. A couple of thoughts:
The variable you are storing to access the game object is a reference to the object...it points to the object. If you reassign it, you just point it a new game object. It does not copy the object. So accessing an object multiple times takes little extra memory. This is true of most classes in Unity. There are some exceptions. For example the Vector3 class is one exception. If you see 'struct' in the script reference, then you will be getting a copy unless you take coding steps to avoid it.
Walking a chain like argo[6].obj.transform.position take a bit more processing time, but unless it is in some sort of loop where it will be done 1000's of times its not worth worrying about.
Thanks again robertbu!! I just finished my first script that I've ever attempted. It runs fine on my computer with just the bare $$anonymous$$imum mock up of the scene that I want to create (with no graphics, just bare bones). I believe that it requires a lot of processing power though, because of the lerp function being called so much for the regular movement of the game. But I need something like lerp (as opposed to rigid body)because of how customized and precise my movements have to be. Since I'm brand new at this, I don't know wether or not the script will run on a broad spectrum of platforms and I would like it to be mobile friendly in the end.
Just because you seem to be the most helpful guy in this community, I was hoping that you could take a glimpse at my script to give me your opinion. I'll P$$anonymous$$ you anyways and if you don't feel up to it, no worries.
Thanks for everything thus far.
The processing of a Lerp() is trivial. Use of the physics for movement (Rigidbody) uses orders of magnitude more processing power. When I have performance concerns, I try to find a way to test things. In your case, I suggest you replace all of your Lerp() call with a $$anonymous$$yLerp() as follows:
Vector3 $$anonymous$$yLerp(Vector3 from, Vector3 to, float t){
Vector3 v3T;
for (int i = 0; i < 1000; i++)
v3T = Vector3.Lerp (from, to, t);
return v3T;
}
The replacement makes 1000, Lerp() calls for every $$anonymous$$yLerp(). Increase or decrease the number in the 'for' loop until it causes a slowdown. I'm guessing it will be 100,000 or 1,000,000.
Note Lerp() is a very simple calculation:
t = $$anonymous$$athf.Clamp01(t);
return A + (B - A) * t;
As for looking at your code, a general code review is not really a UnityAnswers kind of post. But what you can do is to figure out your specific concern(s) and post an appropriate amount of code and asking those questions:
"I'm concerned about the performance of this body of code. Do I need to be concerned and how might I improve it?"
or
"I'm accessing other game object in this way. Is there a better way or problems with what I'm doing?"
That way you'll get feedback from various educated eyes.
Ok, thanks a mill for your lerp idea. I'll check it out.
Sorry for the non 'Answers' type of request.
Follow this Question
Related Questions
Scripts accessing one another (JS) 0 Answers
Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers
Assigning current color to a variable for fade out (C#) 0 Answers
Accessing Variables within another gameobject's script 2 Answers
Communication between objects and other scripts, variables and properties 1 Answer