Hi everybody
I’ve made a script which needs 2 variables to work, its own transform and the transform of an prefab with the tag Player. But the script can’t reach the transform of the prefab which is instantiated by this script:
var classselected : boolean = false;
var spawnplace : Transform;
var archerobject : GameObject;
var mageobject : GameObject;
var warriorobject : GameObject;
function OnGUI () {
if(classselected == false){
GUI.Box(Rect(0,0,300,500),GUIContent("Choose your class wisely"));
}
if(GUI.Button(Rect(100,100,100,100),GUIContent("Archer")) && classselected == false ){
var instance : GameObject = Instantiate(archerobject, spawnplace.position, spawnplace.rotation);
classselected = true;
}
if(GUI.Button(Rect(100,200,100,100),GUIContent("Mage")) && classselected == false){
var instance2 : GameObject = Instantiate(mageobject, spawnplace.position, spawnplace.rotation);
classselected = true;
}
if(GUI.Button(Rect(100,300,100,100),GUIContent("Warrior")) && classselected == false){
var instance3 : GameObject = Instantiate(warriorobject, spawnplace.position, spawnplace.rotation);
classselected = true;
}
}
function Update (){
if (classselected == true){
Destroy(gameObject);
}
}
This script instantiates 3 different prefabs, all with the tag Player. And this is the script which needs the variable othertransform, the transform of 1 prefab with the tag Player:
static var aggroDistance = 10;
var mytransform : Transform;
var otherobject : GameObject;
var othertransform : Transform;
var glitchCorrector : boolean = true;
var tickerInterval : boolean = false;
var tickerRate = 5.0;
private var nextTick = 0.0;
var drop : GameObject;
static var enemyHealth : int = 100;
static var seppuku : boolean = false;
function Update () {
othertobject = GameObject.FindWithTag("Player");
othertransform = otherobject.GetComponent(Transform);
if (Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && glitchCorrector == true){
ArcherHealthScript.curhealth += 10;
WarriorHealthScript.curhealth += 10;
MageHealthScript.curhealth += 10;
glitchCorrector = false;
}
if (Vector3.Distance(mytransform.position, othertransform.position) > aggroDistance && glitchCorrector == false){
glitchCorrector = true;
}
if(Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
ArcherHealthScript.curhealth -= 10;
MageHealthScript.curhealth -= 10;
WarriorHealthScript.curhealth -= 10;
tickerInterval = true;
}
if(tickerInterval == true && Time.time > nextTick){
nextTick = Time.time + tickerRate;
tickerInterval = false;
}
if(enemyHealth <= 0){
seppuku = true;
Destroy(gameObject);
var instance : GameObject = Instantiate(drop, transform.position+Vector3(0,0.2,0), transform.rotation);
ExperienceScript.curexp += 10;
}
}
I think the problem is FindWithTag or .GetComponent, but I’m not very sure.
Please help me solve this problem!
Where is the error raised ? othertransform is null ? Have you tried the function FindGameObjectWithTag ? I'm not quite sure of the difference between the two ...
– BerengerIt just says in the error bar: Variable otherobject has not been assigned. It also displays: Variable othertransform has not been assigned. So I think the mistake is in the FindWithTag function. FindGameObjectWithTag also doesn't work. I'm really sure the Prefab has the tag Player.
– Romano185