- Home /
Transform handle for prefab and getting to a prefab script variable?
New to Unity.... Need some help with correct syntax on accessing a script variable in a prefab to which I have a transform handle.
I have code below in a "GameMaster" script. This script instantiates a prefab with a Transform handle. I want to get at a script variable that is in a script that is part of the same prefab.
The line 'badGuy.Enemy1_AI.EnemyLevel = badEnemyLevel;' in the GameMaster script fails with 'Enemy1_AI is not member of unity.transform'.
I understand this since I have declared badGuy as a Transform which is a sub-component. What would be the correct syntax to access a script variable (Enemy1_AI is the script) when I have a reference to a Transfrom that is part of the prefab?
// 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;
Answer by swhite · Sep 20, 2010 at 05:19 PM
Make sure you check out some of the reference and user manuals for Unity; you'd be surprised the quality of guides they've put together!
http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html
var enemy1 : Transform; var badGuy : Transform;
var badEnemyLevel : int = 1;
badGuy = Instantiate ( enemy1, Vector3 ( (Random.value * 400) - 200, 0, -2460), Quaternion.identity);
*badGuy.gameObject.GetComponent(<scriptname>).EnemyLevel=badEnemylevel;*
I have looked over some documentation. I had looked at the material for the link you provided. Haven't found my answer yet...
I edited my answer to show you a bit of an example; I think that's what you're looking for.
$$anonymous$$y script is as follows: badGuy.gameObject.GetComponent(Enemy1_AI).EnemyLevel = badEnemylevel;
The error I now see:
Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js(76,77): BCE0019: 'EnemyLevel' is not a member of 'UnityEngine.Component'. (Filename: Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js Line: 76)
How to fix?
$$anonymous$$y script is as follows:
badGuy.gameObject.GetComponent(Enemy1_AI).EnemyLevel = badEnemylevel;
The error I now see:
Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js(76,77): BCE0019: 'EnemyLevel' is not a member of 'UnityEngine.Component'. (Filename: Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js Line: 76)
How to fix?