x


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!

more ▼

asked Feb 11 '12 at 06:15 PM

Romano185 gravatar image

Romano185
35 13 20 22

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 ...

Feb 11 '12 at 07:32 PM Berenger

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.

Feb 11 '12 at 07:55 PM Romano185
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Feb 11 '12 at 08:12 PM

Berenger gravatar image

Berenger
11k 12 19 53

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 12 '12 at 01:00 PM

Romano185 gravatar image

Romano185
35 13 20 22

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1668
x1252
x405
x33

asked: Feb 11 '12 at 06:15 PM

Seen: 1340 times

Last Updated: Feb 12 '12 at 01:00 PM