• 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
Question by jimjames · Feb 22, 2015 at 07:17 PM · raycasttargetefficiencyonmouseover

OnMouseOver or Use a Raycast?

I am just wondering what would be more efficient. Adding a script to an object with a OnMouseOver function that access a static variable, or using a raycast system with GetComponent? This is for a targeting system for my game. Static variable can be used in this instance.

Thanks: James

Comment
steve-thud
tonytopper

People who like this

2 Show 1
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 Mmmpies · Feb 22, 2015 at 08:07 PM 0
Share

Don't use Static variables unless you're accessing something like a control script that appears in every scene.

Really you could get either to work without using statics, do you want to elaborate on what you're trying?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer Wiki

Answer by LaireonGames · Feb 22, 2015 at 08:19 PM

Golden question, does it matter? The one thing I have been taught and stand by as a golden rule to development, if its not broke, don't fix it.

I use a combination of both systems over a few projects and never noticed a problem with either. My gut would say that OnMouseOver will be a tad more optimised since its hooked to the engine and can benefit from performance gains on the C++ side but honestly its probably minute.

Just go with whichever system you are more comfortable/familiar with and don't worry about micro-optimisations unless there is a problem.

Though as an FYI GetComponent will always be a bit slow, nothing major but I avoid it as a rule and pass references where possible instead.

Comment
Kerihobo
jimjames
flashframe
steve-thud
tonytopper
ailuropoda0
undroidvedaterol

People who like this

7 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 Kerihobo · Feb 22, 2015 at 09:26 PM 0
Share

@K2Games: "nothing major but I avoid it as a rule and pass references where possible instead."

how does this "pass references" thing work? I'm only aware of GetComponent. How does your method work? I am interested in learning new ways to do things that are supposedly faster. Like how woudl you type the code to "pass reference"?

avatar image LaireonGames · Feb 22, 2015 at 09:34 PM 0
Share

Its not strictly true but basically every variable of a class or object is passed as a reference in C#

So basically:

 class Example : MonoBehaviour
 {
 public  Animation animation;
 public Enemy enemy;//these are basically passing reference. By making them public you can assign the values in the editor
 
 void Update()
 {
 enemy.Hurt();
 animation.Play();//this way you are speaking to the class directly
 
 
 GetComponent<Animation>().Play();//whereas this way Unity has to search for the animation component each time this is called. To be fair for what it does it is very optimised but it will always be slower than using the reference directly
 }
 }

Even calling gameObject or transform is just a quick way of typing getComponent() so if you are really looking for frames these can be stored as well.

avatar image jimjames · Feb 22, 2015 at 09:57 PM 1
Share

Thank You so much for an honest answer without going all technical and talking about problems that are totally irrelevant. Im going with the static variable due to it not being a problem for this project. Thank you agian, James.

avatar image LaireonGames · Feb 22, 2015 at 10:08 PM 0
Share

No worries glad i could help :)

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

function OnMouseOver() ignore selected raycast 1 Answer

UI element in 3D space, reacts to click despite raycast target being infront 0 Answers

RAYCAST Forward error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 1 Answer

Follow Nearest Target 1 Answer

Look at enemy 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