AddToScore problem with prefab

hi there,

I have a strange problem with prefab.

Let me tell you first what I have and what I need.

In a shooting game I want to instantiate enemies while the game progresses and when the player shoots an enemy add one point to the score (which is invisible for now, I don’t have a guiText yet).

to try things while I’m setting up this game, I created an empty gameObject (Score1) and attached the following script to hold the information of the score.

var score = 0;

function AddToScore () {
	
	++score;
	
}

then I created a sphere (as an enemy) and attached the following script in order to add a point to the score of Score1 object when it’s triggered.

var score: Score1;

function OnTriggerEnter () {
	
	score.AddToScore();
	
}

in order to work I attached the Score1 object to the exposed score variable of the script above.

(I used as a guide the marble tutorial and it works fine!!)

but… as I told you I want to instantiate spheres like those as enemies while the game progresses so I created a prefab in the project view and draged on it the sphere from the hierarchy window. when I did that I realized that the Score1 object is not attached to the exposed score variable. I tried to drag n drop again the Score1 object on this exposed variable but it doesn’t “snap” to it. Very strange! Am I doing something wrong???

It’s seems that in general, objects from the hierarcy window cannot be assigned to the inspector window of objects from the project window. I don’t know if I’m wrong though…

ayway, I found another way of doing that. after creating the enemy prefab, I put the prefab in the hierarchy window and place it in the scene view behind the camera so as not to be seen. Then when I instantiate the enemies I use that prefab (from the hierarchy) and not the prefab from the project window. Now the Score1 can be assigned to the inspector and the addtoScore function works!! I hope I’m doing it the right way and that it’s not a solution that will cause problems later.

I found a “scripting” way of Adding to score without placing the prefab in the game scene. For anyone who wants to see how it’s done (or how I have evolved in scripting :P), here:

the empty gameobject “Counter” is in the scene view and holds the information of the score. The following script (Score.js) is attached to this object:

var score = 0;

function AddToScore () {
	
	++score;
	
}

the enemy that is instantiated is a prefab in the project view (assets) and has this script attached to it(AddPointToScore.js)

var counter : GameObject;

function Start () {
	
	counter = GameObject.Find("Counter");
	
}

function OnTriggerEnter () {
	
counter.GetComponent(Score).AddToScore();
	
}

…and everytime an enemy is destroyed a point is added to the score, having the score object in the scene and the enemy prefab in the assets! Cool, huh?

However, I feel that there must be a more sophisticated way of doing this. So, just to improve our programming knowledge, if someone else more experienced wants to share it with us…!! :roll:

You can drag assign objects from the hierarchy view to the inspector, it’s just that you must use the proper data type. In your first case you were using a type of Score1 which is not a data type used in the hierarchy view, so instead you should use GameObject. You did that in your second example except you stopped short of using drag-assignment. Leave your second script as-is but remove the call to find the needed game object (remove the Start function as shown) and instead drag-assign the “Counter” game object to your variable counter so you don’t have to call GameObject.Find().