- Home /
Question by
keiranlovett · Oct 26, 2013 at 06:26 AM ·
variableenumaccessing from any scriptstatic script
Trying to access gameobject variables
I am trying to access a variable for game objects using the below script:
void RefreshCameras() {
int i;
for(i=0;i<playerWeapons.Length;i++) {
if(i==camIdx) {
playerWeapons[i].GetComponent(turretController).controllerType = controllerTypes.Mouse;
} else {
playerWeapons[i].GetComponent(turretController).controllerType = controllerTypes.Empty;
}
}
}
playerWeapons is an array with gameobjects attached (the turrets) which are controlled by another script that has this variable that I want to alter with the above:
public enum controllerTypes {Mouse = 1, AI = 2, Empty = 3 }
public static controllerTypes controllerType = controllerTypes.Mouse;
However all I'm getting is errors. Can someone show some insight?
Comment
You need to either cast or use a different form of GetComponent():
playerWeapons[i].GetComponent< turretController >().controllerType = controllerTypes.$$anonymous$$ouse;
Your answer
![](https://koobas.hobune.stream/wayback/20220613120643im_/https://answers.unity.com/themes/thub/images/avi.jpg)