x


Prefab (Project Folder) to a GameObject (on the Scene)

Hello,

I am trying call another function which is attached to a gameobject( On the scene). But I want to call that function from a Prefab. This prefab is instantiated during run time.

Reason:

I am checking for collision on one script (on Prefab). If there is a collision on one then I want to call another function (on a gameobject) which would update the SCORE for me. In this way I could display variable carrying the score through OnGUI function.

Code:

The script on the prefab - cannondestroy.js

var detonateObject : GameObject; 
var score : la_point;


function OnCollisionEnter(hit : Collision)
{
    if(hit.gameObject.tag == "floor" || hit.gameObject.tag == "enemy"  )
    {
    //Checks if floor is hit
    var detonate = Instantiate (detonateObject,gameObject.transform.position , Quaternion.identity);
    Destroy(gameObject); //Destroys this gameObject if true.

/*

        if(hit.gameObject.tag == 'enemy')

        score.enemyhit();

        else

        //check for hattrick errors. score.enemymiss();

        */
      }
}

Code Explanation:

As per the code - it checks for collision. It checks for collision both on the floor and enemy. If the enemy is a hit I wish to update the scoreline else if the floor is the hit I wish to increase the miss score. The other script that is attached onto to gameobject on every scene would be la_point.js.

My Perception:

I am using a lot of scenes. With the prefab fixed, I could alter the variables on the gameobect which is one the scene view for every scene. The script is going to the same but scores will start from zero always when a new level is loaded.

TRYOUTS:

I tried to attach the script which has the gameobject onto the script on the prefab (again on project folder). But it did not work. I see that I can't do that too.

I humbly REQUEST for help as I am trying solve this which is every important information that has to be given to the user.

THANK YOU very much in advance

more ▼

asked Dec 09 '11 at 12:03 AM

Karsnen gravatar image

Karsnen
736 26 45 54

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

2 answers: sort voted first

you could do:

//Other stuff
    if(hit.gameObject.tag == 'enemy'){

var script = GameObject.Find("name of game object. this could mean it's own name.").
GetComponent(scriptName);
    score.scriptName =+ 10;

    }else{

    //Same thing but with other stuff. I think I told you everything needed.


  }
}

hope that helps. I don't get static stuff myself so I don't bother with that. plus, unless you use DontDestroyOnLoad, you should be fine with it resetting.

I'm not completely sure that you can modify the variable but you probably can.

more ▼

answered Mar 24 at 04:24 PM

Tpaley gravatar image

Tpaley
18 2 2

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

You can use a static variables and functions to access the score script.

// Score.js
static var instance : Score;
var score : int;

function Awake() {
    instance = this;
}

static function EnemyHit() {
    instance.score += 10;
}

// YourOtherScript.js    
function OnCollisionEnter(hit : Collision) {
    if(hit.gameObject.tag == 'enemy')
        Score.EnemyHit();
}
more ▼

answered Dec 09 '11 at 12:19 AM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

But I wish to bring down the values to zero every time I load a new scene.

So when I load a new scene should I just call a random awake function to revert back the values to 0?

Dec 09 '11 at 12:25 AM Karsnen

instance = this;

Could I please know what the above statement actually does?

Dec 09 '11 at 12:29 AM Karsnen
(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:

x5061
x3723
x2080
x1253
x819

asked: Dec 09 '11 at 12:03 AM

Seen: 840 times

Last Updated: Mar 24 at 04:24 PM