x


Updating variable on another script. It is not working.

Ok so i have my character going around and he his supposed to get life whenever he hits a Tool Tool Box. Anyway, i'm trying to display my character life but something it isn't right...

In the tool box i have this script called TesteTool.js:

> #pragma strict
> 
> var vida = 50;
> 
> function Start() { Debug.Log(vida); }
> 
> function OnTriggerEnter (other :
> Collider) {
> 
> if(vida<=80) { vida = vida+20;
> Debug.Log(vida); } else {
> vida=100; Debug.Log(vida); }
>     Destroy(gameObject); }

P.S. - Vida means life in Portuguese.

Well i know life was supposed to be attached to the character but it doesn't matter in this case. Well on the other hand i have a script attached to the camera with this:

#pragma strict
@script ExecuteInEditMode()

var vidaOnGui;

var width = 70;
var height = 25;

function Update()
{
 vidaOnGui=GameObject.Find("ToolBox").GetComponent(TesteTool).vida;
}
function OnGUI()

{
     GUI.Box(Rect(0,0,width,height),"Vida:" +vidaOnGui);
}

Well now everything works fine and when i press play i get the life showing on my left corner, hmmm, almost. When i go into the tool box i get a NullReferenceException error. Why is that and how can i fix it?

Oh and by the way, just for curiosity, but what happens if i have more than one object with the same name in the hierarchy? I mean, when i ask to get the component of the object with that name? And this is not always fixable, because, in example, when you instantiate prefabs along the scene they all get the same name. Am i missing something here?

Glad to be among such nice people like you guys. Please help me. ;) Thanks.

more ▼

asked Jul 09 '12 at 08:07 PM

Satamanster gravatar image

Satamanster
19 1 6 8

I don't know if it helps but debug.logging the life returns that when i enter the trigger the life keeps updating +20 forever...

Jul 09 '12 at 08:18 PM Satamanster
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Try to use static typing:

var some:int;

your var videOnGui is not typed.

weirdly pragma strict should not let you go.

Try to use :

var vidaOnGui:int;

The other one works because you provide an additional info:

var some=10;

Unity sees 10 and knows it is an int.

Edit: Try this:

#pragma strict
@script ExecuteInEditMode()

var vidaOnGui:TesteTool;

var width = 70;
var height = 25;

function Start()
{
 vidaOnGui=GameObject.Find("ToolBox").GetComponent(TesteTool);
}
function OnGUI()
{
     var laVida=vidaOnGui.vida;
     GUI.Box(Rect(0,0,width,height),"Vida:" + laVida);
}
more ▼

answered Jul 09 '12 at 08:24 PM

fafase gravatar image

fafase
10.5k 9 15 40

Ok, thanks a lot, now the error is gone, but still the life isn't being updated on the gui box, it does in the console but not int the box. Can you help me on that aswell? Don't worry, when my question is fully filled i'll mark your answer as correct. Just wan't to ask you aswell to vote up in my question just for the matter that i'm doing a work for college in unity and since i'm new here, i have to wait for my questions to be accepted in the queue before being submitted, would you help me out getting those 15 points by voting in my question? Most appreciated. :D Thanks a lot.

Jul 09 '12 at 08:33 PM Satamanster

Hmmm. Your code worked, i do understand it, i just don't get why does that work and mine doesn't. if you can explain me that it would be great. Now just one left thing to go... Remember that question i made at last in my post... Well it matters here because, imagine, if i duplicate my toolbox now, and you press play. Imagine you life starts at 50, after i hit the first toolbox it updates to 70, but after you hit a second one, nothing occurs. Do you have a solution for me please? :D Thanks again.

Jul 09 '12 at 08:57 PM Satamanster
(comments are locked)
10|3000 characters needed characters left

Let it be, i just figured it out. Actually, i deleted almost everything and did it the opposite way, i was being so stupid doing things this way, so i tagged my toolbox prefab and then putted them wherever i wanted. Then attached the script to my character(is a tank), and set it to display the life in the update of that character... Solve 3 problems in one script... eheheh, Thanks a lot for the help fafase. :D

more ▼

answered Jul 10 '12 at 12:56 AM

Satamanster gravatar image

Satamanster
19 1 6 8

(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:

x3328
x1253
x821
x303
x202

asked: Jul 09 '12 at 08:07 PM

Seen: 353 times

Last Updated: Jul 10 '12 at 12:56 AM