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.

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 :slight_smile:

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

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.
78856-screen-shot-2016-09-24-at-70532-pm.png
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…
78858-screen-shot-2016-09-24-at-70545-pm.png
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 :smiley: (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();
	}