- Home /
Access to script global var's from a transform object? (specific ? on something that should work)
New to Unity.... My problem seems a bit different from other posts on acccessing script variables.
I have code below in a script.
The line 'badGuy.Enemy1_AI.EnemyLevel = badEnemyLevel;' fails with 'Enemy1_AI is not member of unity.transform'.
I am missing something, how can I correct this?
// GameMaster.js
var enemy1 : Transform; var badGuy : Transform;
var badEnemyLevel : int =1;
badGuy = Instantiate ( enemy1, Vector3 ( (Random.value * 400) - 200, 0, -2460), Quaternion.identity);
badGuy.Enemy1_AI.EnemyLevel = badEnemyLevel;
// Enemy1_AI.js
static var EnemyLevel : int;
Please use the proper code tags in the text editor to highlight your code.
Answer by Wotan · Sep 18, 2010 at 07:25 PM
What are you trying to achieve?
badGuy.Enemy1_AI.EnemyLevel = badEnemyLevel;
must fail as badGuy is of the type Transform. Transform however does not have a member .Enemy1_AI.EnemyLevel and therefore the compiler complaints.
In my prefab I have Enemy1_AI script as part of the game object. Enemy1_AI script is properly controlling the actions of this object after I instantiate it Game$$anonymous$$aster. $$anonymous$$y new code is that after I instantiate this prefab in the Game$$anonymous$$aster script I want to set EnemyLevel in the instance of the Enemy1_AI script for the instantiated object.
To do this I need help with the correct syntax to reference and set EnemyLevel in the Game$$anonymous$$aster script.
Answer by Adam Rademacher · Oct 05, 2010 at 12:24 AM
I think this is what you're looking for
badGuy.gameObject.GetComponent(Enemy1_AI).EnemyLevel = badEnemyLevel;
You don't need the .gameObject in there, as Transforms share the GetComponent function.