Hi! When I open my project in OsX and try to compile for iOS I get many compiler errors like it “unknown identifier varName” even though the variable was declared properly and there were no errors in windows, also lines with GetComponent return errors saying that the variable I’m trying to access through it is not part of UnityEngine.Component even though everything worked fine in windows. What’s the deal?
thanks! that solved that part of the issue, however when I try to access other scripts variables like so:
health= GameObject.Find("Player").GetComponent("Player").playerHealth;
I get
it works fine on windows,
playerHealth is declared like so inside the “Player” script of “Player”:
public var playerHealth: float=100;
nope it still gives me the same error
okay I’ve gone a step further to find out what’s going on - here is the error now:
var health : float;
var cPlayer : Component = GameObject.Find("Player").GetComponent("Player");
health = cPlayer.playerHealth; // the same error is now here
EDIT: oops you posted just now - one sec will try what you posted
This is wrong. Those two examples are identical, and will compile fine using #pragma strict, in iOS and Android. The first example is not dynamic typing, it’s type inference. The type is statically inferred from the value you supply.
Also wrong; don’t use quotes in GetComponent. If you use quotes, it can’t figure out what the type is, so it won’t compile.
–Eric
Okay sorry. But i thought the get component can accept a string because that’s what shown in the scripting reference. Unless i interpreted wrongly.
It can, but as the scripting reference says, it’s generally not a good idea. Using a string is only an option for those cases where not using a string won’t work, which the docs also explain. Also, there are more reasons for not using a string that the docs don’t go into, possibly because GetComponent changed in Unity 3.4 (using a string makes it return Component, whereas not using a string makes it return the type that you specify, which is far more useful).
–Eric
Zine92 where did all your posts go?? the one where you suggested (I modified it already)
did the trick for now- this community is awesome thanks zine92!!
final question - does it do the same thing as before, as in if I do
will it only set the local copy of of the health to zero or will the variable that is on Player himself go become 0?
You can just do:
GameObject.Find("Player").GetComponent(Player).playerHealth = 0;
Note the lack of quotes in GetComponent. You can store the reference if you’re using it more than once, in which case you’d do
var nPlayer = GameObject.Find("Player").GetComponent(Player);
–Eric