GetComponent issues

I’m having troubles telling an external component to do something from another script. Here’s the lowdown.

The game I am working on has the ability to switch UI skins through one of the options menus. To accomplish this, I have one game object that persists through all levels. This game object is named Skin. It holds the variable for which skin was last set to use for the entire game. The game object has an attached script named SetSkin that reads as follows:

public var customSkin : GUISkin;

function Start () {
	GUI.skin = customSkin;
}

function GetSkin(){
	return customSkin;
}

Each scene of the game has its own menu object and script that calls upon the persistent Skin object to find out which skin to use.

var currentSkin : GUISkin;

function Start(){
	skinObject = GameObject.Find("Skin");
	skinComponent = skinObject.GetComponent(SetSkin);
	currentSkin = skinComponent.GetSkin();
}

function OnGUI () {
	GUI.skin = currentSkin;

	//do menu stuff here.
}

This method of calling upon the Skin object’s currently active GUISkin works wonderfully in Unity. However, as soon as I bring this code into Unity iPhone, every menu object returns the error
‘GetSkin’ is not a member of ‘UnityEngine.Component’.

i don’t know why it’s not working on the iphone, but you could try a alternative way (if i have understand you correctly)

something like (untested code ahead):

// file CurrentSkin.js

static var currentSkin : GUISkin;

function ChangeSkinFromUserInput() {
   currentSkin=newSkinFromUserInput;
}
// all other scripts

function OnGUI() {
   GUI.skin=CurrentSkin.currentSkin;
}

Thanks, I’ll give that a shot tonight.

Worked like a charm. Thanks again.

Your original script didn’t work because this line doesn’t specify the type of the variable:

skinComponent = skinObject.GetComponent(SetSkin);

In the normal version of Unity, dynamic typing would be used to figure out which type skinComponent should be at runtime, but that’s not supported on the iPhone.

Try this instead:

var skinComponent : SetSkin = skinObject.GetComponent(SetSkin);