Why ain't this working? (541648)

I’ve tried these scripts and everything works. Almost everything…

#pragma strict

var Player : Transform;
var Script : GuiText = Player.GetComponent(GuiText);
var woodCount = Script.woodCount;

function OnMouseEnter () {
    renderer.material.color = Color.green;
}
function OnMouseExit () {
    renderer.material.color = Color.white;
}
function OnMouseDown () {
    Destroy (gameObject,0.1);
    renderer.material.color = Color.red;
    woodCount += 1;
}

and this script

#pragma strict

@script RequireComponent(GUIText)

var TreeDead : Transform;
var Script : Pickup = TreeDead.GetComponent(Pickup);
var woodCount = 0;

function OnGUI () {
    guiText.text = ("Wood:"+woodCount);
}

The woodCount += 1; doesnt really add 1 to the text of the GuiText, is there anyway to fix this? (By the way, im using JavaScript)

The woodCount variable in your first script belongs only to that script. It’s unrelated to the woodCount variable in your second script.

–Eric

But the one from the first is the same as the second, it uses one of the techniches of sharing variables, like as if the second script was the host of the woodCount and the first only uses it, or can’t i add this:

var woodCount = Script.woodCount;

Deleting that line and using Script.woodCount would fix it in some way?

EDIT: Nope, still doing the same thing using the pure script variable reference… Now it is just like this:

#pragma strict

var Player : Transform;
var Script : GuiText = Player.GetComponent(GuiText);

function OnMouseEnter () {
    renderer.material.color = Color.green;
}
function OnMouseExit () {
    renderer.material.color = Color.white;
}
function OnMouseDown () {
    Destroy (gameObject,0.1);
    renderer.material.color = Color.red;
    Script.woodCount += 1;
}

But still no changes, even while referencing a script’s variable directly.

It’s really not. They are different and separate variables that have no connection to each other; all you did was assign the value from one variable to another, which doesn’t make them the same variable. It’s just copying a value. The correct method is to get a reference to the first script and change the woodCount variable directly.

As to why that’s not working, you can start by making sure all code aside from variable declarations is inside a function, since frequently you get unanticipated results if you don’t. Also make variables private if they don’t need to be accessed from elsewhere.

private var scriptReference : MyScript;

function Start () {
    scriptReference = GetComponent(MyScript);
}

–Eric

I changed it, but it is still not working, unless i did something wrong (probably, im still new to scripting anyway)

The Pickup.js:

#pragma strict

private var Player : Transform;
private var Script : GuiText;

function OnMouseEnter () {
    renderer.material.color = Color.green;
}
function OnMouseExit () {
    renderer.material.color = Color.white;
}
function OnMouseDown () {
    Destroy (gameObject,0.1);
    renderer.material.color = Color.red;
    Script = GetComponent(GuiText);
    Script.woodCount += 1;
}

The GuiText.js:

#pragma strict

@script RequireComponent(GUIText)

var woodCount = 0;

function OnGUI () {
    guiText.text = ("Wood:"+woodCount);
}

Sorry for bothering you too much.

I assume those scripts are on two different objects, so GetComponent in that case should result in a null reference exception error, since it refers to the object that the script is on. You need to say which object has the script that you’re with GetComponent. http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

–Eric

Script = GetComponent(GuiText) as GuiText;

Yeah i was getting the NullReferenceException, i only didn’t know it was caused by that…

And thank you, you both! Ill look into that link and that script part of yours but for now thanks a lot!!!

Nope. :wink: GetComponent(GuiText) is already typed as GuiText, no need to cast. (By the way, I’d recommend a more descriptive script name; that one is too close to GUIText, so it’s confusing.)

–Eric

It still doesn’t work. I get these errors:

Assets/Pickup.js(12,38):BCE0077: It is not possible to invoke an expression of type ‘int’.
Assets/Pickup.js(15,17):BCE0020: An instance of type ‘GuiText’ is required to access non static member ‘woodCount’.

Here is the edited Pickup.js:

#pragma strict

var Player = GameObject.FindWithTag("Player");

function OnMouseEnter () {
    renderer.material.color = Color.green;
}
function OnMouseExit () {
    renderer.material.color = Color.white;
}
function OnMouseDown () {
    Player.GetComponent(TextScript).woodCount();
    Destroy (gameObject,0.1);
    renderer.material.color = Color.red;
    TextScript.woodCount += 1;
}

The scripts are really on different GameObjects, TextScript on “Player” and the Pickup on the “Pickupable Object”.
And also i didn’t use the script line of the Unity Technologies member/worker…
Oh and i changed the name to TextScript.js instead of the GuiText by request.

You’ve kind of taken a step backward compared to your earlier script. Now you’re doing GetComponent without assigning the result to anything, and you’re apparently using a variable like it was a function…woodCount() isn’t something that makes sense in this context. Also, again, all code aside from variable declarations should be inside functions, and private if it doesn’t need to be accessed from elsewhere:

private var player : GameObject;

function Start () {
    player = GameObject.FindWithTag ("Player");
}

–Eric

Ok, that fixed one of those errors, but it also came up with 1 new error:
Assets/Pickup.js(18,20): BCE0020: An instance of type ‘TextScript’ is required to access non static member ‘woodCount’.
The other is still the same, only the lines changed: 7,41.
The script is now like this:

#pragma strict

private var Player : GameObject;

function Start () {
    Player = GameObject.FindWithTag("Player");
    Player.GetComponent(TextScript).woodCount;
}
function OnMouseEnter () {
    renderer.material.color = Color.green;
}
function OnMouseExit () {
    renderer.material.color = Color.white;
}
function OnMouseDown () {
    Destroy (gameObject,0.1);
    renderer.material.color = Color.red;
    TextScript.woodCount += 1;
}

Go back to the earlier scripts you had, where you were getting a component and assigning it to a variable. Also look up GetComponent in the docs, and re-read the page about accessing other game objects that I linked to above.

–Eric

Ok i’ll do it and come back with the report.

Thanks!!! Now im using this piece of beauty:

    var TextScript = GameObject.Find("Player").GetComponent(TextScript);

TextScript.woodCount += 1;

and it works! You people really helped me!!!

Man, I was so dumb… looking back at this, I can tell I was still a complete noob at Unity… good thing I’m not like that anymore.

Yep, there are all sorts of new and interesting ways to mess stuff up once you’re no longer a noob, trust me.

–Eric

1 Like