- Home /
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
Answer by Statement · Dec 09, 2011 at 12:19 AM
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();
}
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?
instance = this;
Could I please know what the above statement actually does?
Answer by Tpaley · Mar 24, 2013 at 04:24 PM
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.
Your answer
Follow this Question
Related Questions
How do I assign a prefab reference to a private variable? 1 Answer
How to create 3D game object in specified pixel size? 2 Answers
How to add Forces and Torques relative to an object at a none central position 2 Answers
C# Check If Scripted Gameobject goes Past Variable Gameobject 0 Answers
Setting a game object from a list to be the active character that gets instantiated 1 Answer