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 /
This question was closed Mar 03, 2013 at 07:22 AM by Julien-Lynge for the following reason:

Not appropriate for UnityAnswers, asker will rephrase.

avatar image
0
Question by highpockets · Mar 02, 2013 at 12:09 AM · gameobjectvariablereference-other-object

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:

  1. 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? )

  1. 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?

  2. Create another script that controls the GO and is also attached to the GO?

  3. 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

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 robertbu · Mar 02, 2013 at 03:07 AM 0
Share

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.

avatar image highpockets · Mar 02, 2013 at 07:38 AM 0
Share

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.

avatar image robertbu · Mar 02, 2013 at 05:34 PM 0
Share

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.

avatar image highpockets · Mar 02, 2013 at 09:37 PM 0
Share

Ok, thanks a mill for your lerp idea. I'll check it out.

Sorry for the non 'Answers' type of request.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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