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 PsychoPsam · Sep 18, 2016 at 02:38 PM · unity 5inspectorclassesattributeaccessing

How to select a variable from another GameObject by accessing the class in the Inspector.

Hi all. I have an issue and I'm not sure what code technique I should use to solve it. I have a repair screen (attached) which is populated with different components around the car. However it's never set in stone what that component will be. For example the front weapon will change over time, however if it's a gatling gun or a rail gun it will still have the ammo, repair and damage integers as attributes. I want to make this generalised so that when I click into the front weapon component and select the ammo it's going to increment that value in game. The value however points to another GameObject's script called MoveCar.cs which has a public class in it called CarAttributes. What I want to be able to do is drag the GameObject on the car into a slot under the RepairScreen script component in the Inspector and select say CarAttributes.ammo being a public int from the CarAttributes class. Then it would directly increment that variable when I click on it at runtime. I think this is a place to use [System.Serialisable] on the CarAttributes class but I need to be able to tell the RepairScreen class to specifically access the integer I nominate from the CarAttributes class directly in the inspector which dragged into the RepairScreen inspector component. I think possibly it's a generics solution as well, but I'm not totally sure how to do this. Any advice on what would solve this problem or where I should start researching would be great thanks.alt text

screen-shot-2016-09-18-at-71411-pm.png (129.3 kB)
Comment
Add comment · 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 Adarn · Sep 19, 2016 at 04:24 AM 0
Share

Have a look into interfaces. This is one way that you can implement what you want.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SergioSandiaz · Sep 19, 2016 at 06:00 AM

What I do is that:

 public class Script1 : MonoBehavior {
 public float Fuel;
 }

And then you make a reference from your other script:

 public class Script2 : MonoBehavior {
 public Script1 script;

 void Update() {
 script.Fuel = script.Fuel + 1;
 }
 }

If you made your script under a specific namespace, you need to import it before make the reference

 using...

I know there are better ways, and I would like to know them :)

(and don´t use numbers on your script names)

Comment
Add comment · Show 2 · 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 PsychoPsam · Sep 19, 2016 at 09:01 AM 0
Share

Thanks @SergioSandiaz. I've implemented it in the $$anonymous$$oveCar.cs script like this...

 public class CarAttributes : $$anonymous$$onoBehaviour
 {
     public int frontWeaponAmmo;
 
 }
 public class $$anonymous$$oveCar : $$anonymous$$onoBehaviour {
 
     [Header("Notes about this class")]
 
     public CarAttributes myCarsAttributes;

and then in the RepairScreen class like this...

 public class HandleSubComponentValue : $$anonymous$$onoBehaviour {
 
     public CarAttributes whichObj;   
     public int $$anonymous$$Range=0;
     public int maxRange=10;
     public int incVal = 1;

But its empty in the $$anonymous$$oveCar component and I can't drag the $$anonymous$$oveCar object in the repair screen to access the attribute :( It's just not there in the Scene. alt text

alt text

screen-shot-2016-09-19-at-32357-pm.png (15.6 kB)
screen-shot-2016-09-19-at-32257-pm.png (20.5 kB)
avatar image SergioSandiaz · Sep 23, 2016 at 09:44 PM 0
Share

First, sorry for the delay, and I think the problem is that you´re asking for a component that your car doesn´t have, so before you fill the variable add the Car Attributes script to your car or what are u using. You can do it with PlayerPrefs too, so check them

avatar image
0

Answer by PsychoPsam · Sep 27, 2016 at 06:49 AM

Okay thanks @SergioSandiaz. In the meantime I had some success using an enum to determine which attribute I needed from an object in the subcomponent script. alt text This pic above shows the sub component (the item to modify) which has an increment function. It points to the PlayersCar object to access these attributes... alt text Then in the sub component I select the attribute I want to access via the enum, which uses a switch statement in the sub component class to select a attribute from the PlayerCars class like this... very verbose but the drag and drop functionality is there and easy to maintain until I solve the problem using a more elegant solution :D (Here's a demo - link text)

 public int GetValue ()
     {
         switch(myCarsAttributes)
         {
             case CarAttributes.FrontWeaponAmmo:
                 return whichObj.GetComponent<MoveCar>().FrontWeaponAmmo;
                 break;
             case CarAttributes.FrontWeaponDamage:
             return whichObj.GetComponent<MoveCar>().FrontWeaponDamage;
                 break;
             case CarAttributes.FrontWeaponRepair:
             return whichObj.GetComponent<MoveCar>().FrontWeaponRepair;
                 break;
             case CarAttributes.ChassisF:
                 return whichObj.GetComponent<MoveCar>().ChassisF;
                     break;
             case CarAttributes.ChassisB:
                 return whichObj.GetComponent<MoveCar>().ChassisB;
                     break;
             case CarAttributes.ChassisL:
                 return whichObj.GetComponent<MoveCar>().ChassisL;
                     break;
             case CarAttributes.ChassisR:
                 return whichObj.GetComponent<MoveCar>().ChassisR;
                     break;
             case CarAttributes.ChassisBody:
                 return whichObj.GetComponent<MoveCar>().ChassisBody;
                     break;
             
         }
         return -1;
     }
 
     public void SetValue (int value)
     {
         switch(myCarsAttributes)
         {
             case CarAttributes.FrontWeaponAmmo:
             whichObj.GetComponent<MoveCar>().FrontWeaponAmmo = value;
                 break;
             case CarAttributes.FrontWeaponDamage:
             whichObj.GetComponent<MoveCar>().FrontWeaponDamage = value;
                 break;
             case CarAttributes.FrontWeaponRepair:
             whichObj.GetComponent<MoveCar>().FrontWeaponRepair = value;
                 break;
             case CarAttributes.ChassisF:
             whichObj.GetComponent<MoveCar>().ChassisF = value;
                 break;
             case CarAttributes.ChassisB:
             whichObj.GetComponent<MoveCar>().ChassisB = value;
                 break;
             case CarAttributes.ChassisL:
             whichObj.GetComponent<MoveCar>().ChassisL = value;
                 break;
             case CarAttributes.ChassisR:
             whichObj.GetComponent<MoveCar>().ChassisR = value;
                 break;
             case CarAttributes.ChassisBody:
             whichObj.GetComponent<MoveCar>().ChassisBody = value;
                 break;
         }
         myField.text = whichValue.ToString();
     }



screen-shot-2016-09-24-at-70532-pm.png (35.3 kB)
screen-shot-2016-09-24-at-70545-pm.png (86.2 kB)
Comment
Add comment · Show 1 · 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 SergioSandiaz · Oct 03, 2016 at 03:09 AM 0
Share

Pretty good bro! thanks for the info too

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Where can I find a complete list of Attributes and how they work and 2 Answers

Animation Editor not seeing (all) the properties of my component 0 Answers

Drawing Order of DecoratorDrawer doesn't work? 1 Answer

Multiple scripts reference to the same SerializeField 1 Answer

HideInInspector with inherited variables 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