Can't get var from other script.

I have two javascript scripts:


 var power : float = 50.0;
 var Mage : GameObject;
 function Update () {
     var other : PunchWall;
     other = Mage.GetComponent("PunchWall");


 var startpunch : float;
 var punchOK : int = 0;
 function Update () {
     if(Input.GetButtonDown("Jump")) {
         startpunch = 35;
         punchOK = 1;
     if(startpunch>0) {
 function OnTriggerEnter(theCollision : Collider){
     if(theCollision.gameObject.name == "Hitbox"){
         if (punchOK == 1) {

And I want to use the variable 'punchOK', which is stated in my 'PunchWall' script and is linked to my player named Mage. (Force is linked to my Brick, which is supposed to fly away after it gets punched.

I put a Debug in the Update and it gives 'Null'. So it doesn't even fill my var other?

more ▼

asked Sep 22, 2011 at 08:26 AM

avatar image

1 6 4 6

Hmmm, Debug still gives Null.

Sep 22, 2011 at 08:58 AM PrincessPiranha

Ok, maybe "other=GameObject.Find("Mage").GetComponent(PunchWall).punchOK;" However, I'm still not sure about that, because I don't have Unity here, in this moment! Hope to help you, this time...

Sep 22, 2011 at 09:12 AM BiG

That's okay, I appreciate any help!

It does give an other error now though: "InvalidCastException: Cannot cast from source type to destination type."

Sep 22, 2011 at 09:31 AM PrincessPiranha

Ah, yes: you are declaring other as "PunchWall",in Force.js, but the istruction that I've posted above returns an integer, now. Remove "var other : PunchWall" and declare "other" directly into the istruction above (var other=GameObject.Find....); or declare "other" as int.

Sep 22, 2011 at 09:39 AM BiG

That worked!!! Thank you so much! :)

Sep 22, 2011 at 09:45 AM PrincessPiranha
show all comments (comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You should do the following to get the punchOk value:

var power : float = 50.0;

function Update () { var other : PunchWall; other = GameObject.Find("Mage").GetComponent(PunchWall); Debug.Log(other); if (other.punchOK==1){ rigidbody.AddForce(Vector3(power,0,0)); } } But it seems the whole thing is wrong: the better way to do that would be to apply the force in OnTriggerEnter or OnCollisionEnter (depends on the brick's collider isTrigger flag).

more ▼

answered Sep 22, 2011 at 10:00 AM

avatar image

59.1k 39 142 334

No I got it working already, thanks to BiG! :)

Sep 22, 2011 at 11:01 AM PrincessPiranha
(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



Answers and Comments



asked: Sep 22, 2011 at 08:26 AM

Seen: 1850 times

Last Updated: Sep 22, 2011 at 11:01 AM