- Home /
Variable of prefab has not been assigned
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 ...
It 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.
Answer by Berenger · Feb 11, 2012 at 08:12 PM
Look's like your instanciation occurs only when the player presses a button. Problem is, in your Update, you are looking for it all the time. FindWithTag will only look for objects instanciated, in the scene, not the prefab in your project folders.
So you should create a public function in the second script to affect those other's variables, and call it from the other script. In the first script's update, check if othertobject is null and leave the function if it is.
Answer by Romano185 · Feb 12, 2012 at 01:00 PM
Problem solved! Just made a small mistake, said other*t*object instead of otherobject. But I've made classselected static and added if(classselected == true) to the second script right before FindWithTag, just to be safe
Your answer
Follow this Question
Related Questions
Get unity to recognize prefab in C# 2 Answers
Set a variable in another object at point of instantiation. 1 Answer
Instantiate Prefabs. Errors. 2 Answers
Play iTween on instantiated prefab 1 Answer