Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by shining_gentleman · Jul 31, 2019 at 12:33 PM · methodreferencingpassing

Passing Object References to Other Script

I have a scriptable object with several GameObject variables which I fill in the inspector with some prefabs. They are parts of armor I'd like to instantiate to my player instance by calling a method inside the player script. So far I made it work by passing the references with a method call inside the scriptable object. To make it simple, I wrote something similar like this:

public class ScriptableArmorObject : Equipment { Player player;

 public GameObject helmet;
     public GameObject bodyPart;
     public GameObject legPart;
     
     void Start()
     {
         //Find player and get Player component;
     }
 
     void Equip()
     {
         PassReferences();
         player.SetArmor();
     }
 
     void PassReferences()
     {
         player.helmetRef = helmet;
         player.bodyRef = bodyPart;
         player.legRef = legPart;
     }
 }

I made it work with no problem, but here come my question, is there a better performance-wise way to do this? I mean I have 8 part references to pass, and inside the Player script I also need to declare 8 variables to hold these references. I've tried to pass the ScriptableArmorObject to my Player script and access the references there, but I got some problems when it comes to swap my equipped armor. Hope someone can helps, thank you!

Comment
Add comment · Show 2
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 xxmariofer · Jul 31, 2019 at 12:49 PM 0
Share

a scriptableobject with 8 vars is heavier than just 8 vars. passing 8 vars is really fast and you are not even doing it all the time

avatar image msue2 · Jul 31, 2019 at 01:32 PM 0
Share

What where those other problems you mentioned ? To me it sounds like you should look there.

1 Reply

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

Answer by Esteem · Jul 31, 2019 at 03:22 PM

if you're worried about performance with 16 references on two objects you're probably worrying about the wrong things.

To put things into perspective:

most modern CPUs are running upwards of 3Ghz. CPU such as that is capable of performing 3 000 000 000 cycles per second.

Now forgive me the inaccuracy, but accessing a reference type takes approximately
(depending on many things but for our example it's good enough) 50-150 cycles.

You can think of it as accessing and setting your 16 reference type variables takes approx.:

100 * 16 (*100 for some additional random overhead = very VERY generous) = 16 000 cycles.

3 000 000 000 / 16 000 = 1/187 500 which is 0.00000053 % of your CPU's power at any given frame

now ask yourself:
Do you need to care about performance in such scenario?

Comment
Add comment · Show 10 · 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 xxmariofer · Jul 31, 2019 at 03:32 PM 0
Share

his second approach would be even slower, may i ask you something? do you know how much would cost calling a method vs accessing properties?

avatar image Esteem xxmariofer · Jul 31, 2019 at 05:07 PM 0
Share

I am not sure what do you mean by that. From what I can gather, shining_gentleman is slightly new to program$$anonymous$$g and he fears the performance hit his application will take by doing things such as this. I made a simple comparison to put him at ease because these kinds of performance optimization aren't necessary and in time these issues will get ironed out.

avatar image xxmariofer Esteem · Jul 31, 2019 at 05:24 PM 0
Share

I know, just wanted to point out that having a var if the type of the scriptable object would be heavier than just the 8 vars

Show more comments
avatar image shining_gentleman · Aug 01, 2019 at 02:14 AM 0
Share

Thanks for all your replies guys, it does help me a lot in understanding what I'm actually doing. Well, even though I just started to learn Unity and I have no background at all in program$$anonymous$$g, but I guess I get the Idea of what you guys are trying to explain, I'm truly grateful. To answer @Esteem questions, you've guessed everything right! This equipment system I've been trying to implement for my project is the first complex scripting (at least to me) I've ever done. Others are just bought asset, reading documentation, and following a tutorial. I was just wondering if there is a better way (more programmer-way) in doing that, because that's all the ways I can think about. I'm really interested in program$$anonymous$$g, thus I'd like to know things that I might do wrong as early as possible and improve.

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

110 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 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 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 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 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

ArgumentException: The Object you want to instantiate is null. 1 Answer

Passing a class instance to a method 1 Answer

loop through scripts attached to objects in an array? 1 Answer

How to deep copy a collider2d 1 Answer

trying to access to a variable (m_GravityMultiplier) of the FirstPersonController script from a Child of the FPSController 0 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