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 LSDriller · Sep 06, 2015 at 07:17 PM · c#script.getcomponentvariables

Using a variable value with GetComponent

Hi to all! I'm working on a top-down shooter, and i'm doing good, but till now I didn't care about the code repetition, and I made 5 powerups with pretty much the same script, with some little differences. What I want to do now is to make a single script, who takes all the references it needs, and calls the right methods to apply the right powerup. To activate bonuses, I'm trying to use a simple code like this one:

 public void BonusON(string bonusName)    
 {    
         bonusDuration = GameObject.FindGameObjectWithTag(bonusName)
         .GetComponent<scriptName>.bonusDuration; 
 
        //other code        
 }

What i'm trying to do is to reproduce something I saw while I was learning PHP, like dinamically obtaining a variable name using another variable value, then use the variable name to call a different method I prepared before... I made a research online to find if this way could be used in c# as well, but without luck. What I surely need to know is if there's a way to use the GetComponent method as I wrote above, so giving it a variable and letting it use that variable value as a component name.

Or, if there's a simpler way, of course.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eudaimonium · Sep 06, 2015 at 08:20 PM

Above code should work, except you're missing brackets for actual function call on GetComponent method:

 bonusDuration = GameObject.FindGameObjectWithTag(bonusName).GetComponent<scriptName>().bonusDuration; 

I would suggest against using "Find" functions within gameplay time, though, official docs say it's quite slow and can cause performance hiccups. Use any other method of obtaining a solid reference, for example if you're activating something while colliding with it, obtain the reference to the object via Collision argument.

Or if at all possible, use public variables and assign references via Inspector.

Comment
Add comment · 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 LSDriller · Sep 06, 2015 at 09:53 PM 0
Share

Yeah, my fault about the missing brackets... So, you're telling me that if scriptName is a string variable who have for value the tag of a gameObject, this syntax could work?

avatar image Eudaimonium · Sep 06, 2015 at 10:05 PM 0
Share

Yes, but like I said, avoid using "Find" functions. This will simply return the first GameObject with the corresponding tag. If there are multiple ones, any one could be returned. But syntax could theoretically work, yes.

Any particular reason why you aren't simply trying this out for yourself, see if it compiles and how it works, and ins$$anonymous$$d asking on the Answers first?

avatar image LSDriller · Sep 07, 2015 at 07:04 PM 0
Share

Well, to be honest, I tried before asking, but I thought I was making some mistake with syntax, because I was always getting a null value, or an error about a missing reference :P

avatar image Bunny83 · Sep 07, 2015 at 07:21 PM 0
Share

Actually, no, scriptName can't be a variable.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Syntax troubles (GetComponent and changing variables) 1 Answer

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer

Calling variable accross scripts in C# problem (Closed) 1 Answer

Random selection 1 Answer

Getting a Variable on Script B to equal a variable from script A 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