- Home /
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.
Have a look into interfaces. This is one way that you can implement what you want.
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)
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.
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
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. 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... 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();
}