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 /
  • Help Room /
avatar image
0
Question by Niodyan · Aug 11, 2017 at 07:20 AM · scripting problemscript.componentmonobehaviour

accessing a script using a variable with getComponent, then accessing a variable inside that script

Hello. I'd like to know if something I am attempting is actually possible (C#):

MonoBehaviour monobehaviour; int theCapacity; theCapacity = combineMag1.GetComponent ().currentCapacity;

I'd like it to be so that i can assign a variable to access a specific script, and then use that variable to access a variable inside that script. If anyone knows how please help, thanks

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 UnityCoach · Aug 02, 2017 at 11:03 AM

Yes, simply use

 YourComponentType comp = GetComponent<YourComponentType>();
 comp.yourVariable = the value;

or all on one line if you're sure the component is there.

 GetComponent<YourComponentType>().yourVariable = the value;
Comment
Add comment · Show 5 · 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 Niodyan · Aug 02, 2017 at 11:24 AM 0
Share

Thanks very much for the fast reply. Can you clarify what you mean by YourComponentType? The component is just a script or monobehaviour, so what would I use? Thanks again for the reply

avatar image UnityCoach Niodyan · Aug 02, 2017 at 11:28 AM 0
Share

When you declare a $$anonymous$$onoBehaviour, it has a type

 public class PlayerHealth : $$anonymous$$onoBehaviour // PlayerHealth is the type here
 {
     public int health = 100;
 }

So you can do

 GetComponent<PlayerHealth>().health -= 1;
avatar image UnityCoach · Aug 02, 2017 at 11:58 AM 0
Share

If your intent is to access a given property on any $$anonymous$$onoBehaviour or Script, you would have to use Reflection.

Something in the likes :

 using System.Reflection;
 
 $$anonymous$$onoBehaviour[] all$$anonymous$$onoBehaviours = GetComponents<$$anonymous$$onoBehaviour>();
 foreach ($$anonymous$$onoBehaviour mono in all$$anonymous$$onoBehaviours)
    
 {
    
     PropertyInfo[] allProperties = mono.GetType().GetProperties (BindingFlags.Instance | BindingFlags.Public);
     for (int i = 0; i < allProperties.Length; i++)
    
     {
         allProperties[i] // query the property to see if it's the right type/name, etc..
     }
 }

This comes at a cost and isn't supported on all platforms. I find it mostly useful for Editor tools.

avatar image Niodyan UnityCoach · Aug 08, 2017 at 03:14 PM 0
Share
         if (combine$$anonymous$$ag1.name == "pistol$$anonymous$$agItem") {
             //create variable that allows me to access the script in the if statement below
             haveComponent = true;
         }
 
         if (haveComponent == true) {
             if (combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().currentCapacity != combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().maxCapacity) {
                 maxToReload = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().maxCapacity - combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity;
                 if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity > maxToReload && processed == false) {
                     combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload;
                     combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity - maxToReload;
                     processed = true;
                 }
                 if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity == maxToReload && processed == false) {
                     combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload;
                     processed = true;
                     Destroy (combine$$anonymous$$ag2);
                 }
                 if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity < maxToReload && processed == false) {
                     combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity;
                     processed = true;
                     Destroy (combine$$anonymous$$ag2);
                 }
             }
         }

How would I alter this code to make it work? The reason I do this is because it would be much more optimised to just assign the script in a variable then to write the whole code out over and over and over accessing different scripts.

avatar image Niodyan UnityCoach · Aug 11, 2017 at 06:52 AM 0
Share

I sent a reply yesterday but I don't think it went through (well i dont see it soooooo). The following is my script: if (combine$$anonymous$$ag1.name == "pistol$$anonymous$$agItem") { //create variable that allows me to access the script in the if statement below haveComponent = true; } if (haveComponent == true) { if (combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().currentCapacity != combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem>().maxCapacity) { maxToReload = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().maxCapacity - combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity; if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity > maxToReload && processed == false) { combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload; combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity - maxToReload; processed = true; } if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity == maxToReload && processed == false) { combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + maxToReload; processed = true; Destroy (combine$$anonymous$$ag2); } if (combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem>().currentCapacity < maxToReload && processed == false) { combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity = combine$$anonymous$$ag1.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity + combine$$anonymous$$ag2.GetComponent<pistol$$anonymous$$agItem> ().currentCapacity; processed = true; Destroy (combine$$anonymous$$ag2); } } }

as you can see i use .getcomponent

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

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

Related Questions

reference to monobehaviour in static script,declaring a static class/script in a monobehaviour 0 Answers

How can you delete a MeshCollider?? CAN you delete a MeshCollider? 1 Answer

Disabling a script while pressing a key down 2 Answers

CAMERAS ERROR , I NEED HELP 0 Answers

there MonoBehaviour script data type? 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