- Home /
Get A Variable from another Script in Unity iPhone
Hi I have 2 scripts A and B.I'm working on A and I want to get a variable from B. Here is the Code:
animationIsPlay = gameController.GetComponent("GameControl").animationIsPlay;
It works in Unity PC version quite well,but in Unity iPhone it always told me "animationIsPlay' is not a member of 'UnityEngine.Component". Can someone tell me what is the problem?? How to solve it ?Thanks a lot.
Answer by Peter G · Jul 27, 2010 at 03:23 PM
You cannot use dynamic typing with Unity Iphone. There are several ways to fix this. The simplest, may be to use as
to cast to the correct type, but if you want good performance and will be using GameControl a lot then the fastest way is to cache a link in the Start function then you don't have to look it up when you need it. That is actually one of the most import optimizations you can make on the iphone. You should also do it with things like transform, rigidbody, characterController etc.
So, try changing your script to this.
var gameControl : GameControl;
function Start () { gameControl = gameController.GetComponent("GameControl"); //Now you have a link to GameControl and don't have to look it up whenever you call the function. }
function Update () { //it could be whatever function name animationIsPlay = gameControl.animationIsPlay; }
Answer by equalsequals · Jul 27, 2010 at 03:13 PM
Try:
animationIsPlay = (gameController.GetComponent("GameControl") as GameControl).animationIsPlay;
Cheers
==