Problem with calling function in a GUIText script from another script C#

Hi there,

I’ve made a very short FPS game where there are 3 blocks in the centre of the screen. The player shoots bullets at the balls and when they collide, the blocks get a rigidbody attached to them and fall to the ground. The bullets get destroyed on contact with the blocks.

However, I’m trying to use the collision between the bullets and the blocks to call a function in a GUIText script to increase a variable called count (which is the score). I was wondering if someone could help me with where I might be going wrong.


Force.cs //attached to my prefab bullets

public Score Counter;

void OnCollisionEnter (Collision col)

{
if (col.gameObject.name == “Target”)
{
Debug.Log (“Target hit!”);
col.gameObject.AddComponent(“Rigidbody”);
Destroy(gameObject);

Score Counter = GetComponent(); // I’m getting the script ‘Score.cs’ and placing it to Counter.
Counter.Count(); // Calling the Count() in Counter.
}
}


Score.cs // This script is attached to the GUIText

public int count = 0;

public void Count ()
{
count++;
}

When I run the game, everything seems to work apart from the increase in the count variable. I do get this error though:

NullReferenceException: Object reference not set to an instance of an object
Force.OnCollisionEnter (UnityEngine.Collision col) (at Assets/Force.cs:29)

Would be grateful for any advice or direction on what I should read up on.

You declare a variable inside Force.cs named “Counter” but then that gets overridden by declaring a local “Counter” inside OnCollisionEnter

Also GetComponent only returns a component attached to the same object - do you have a Score.cs script attached to your bullet?

Thanks for spotting the Score Counter error. I’ve corrected this. I’ve also removed ‘Destroy(gameObject)’ because I believe it was affected my count value.

I think I should have mentioned that the bullets fired are instantiated on pressing space bar. I’m trying to get the Force.cs script in the instantiated bullet to increase a variable count value in the Score.cs script which is in GUIText. Thus when the count value is incremented by 1, the score displayed on the GUIText will also increase.

So I’m supposed to use the instantiated bullets gameobject and place a script within the bullet gameobjects to produce the GUIText then?
I’ll give it a try.

I’ve solved it!

I rewrote the code and program again. What I was making the code too complicated by trying to add a GUItext to a prefab.

Instead of detecting collision with the prefab and the target using a script on the prefab, I placed a collision detection script on the target blocks.

Next, in each of the target blocks script, (because I have three), I made a script reference to a function called Count() in the script that I had placed in my GUIText.

Put simply, each of my target object has two scripts (Collision detection and GUIText script).

All three targets are linked to my GUIText, Which only has a ScoreCount script and a function called Count(). The function Count() will add 1 to the score everytime it is called (and it would only be called when a collision occurs).

Wow…spent 2 days on getting the GUI figured out but it definitely was worth it!