bronxbomber was nice enough to help me with this script earlier tonight but I’ve tweaked it now so it’s broken …
var scoreText;
var item1 : GameObject;
var item2 : GameObject;
var item3 : GameObject;
var item4 : GameObject;
// this is the item we attach this script to
var item5 : GameObject;
// ths starting score
var CurrentScore = 0;
function scoringMe()
/* This is here to set the GUIText on the screen where you're displaying the score to the variable that holds the score*/
{
scoreText.text = CurrentScore;
}
function OnCollisionEnter( theCollision : Collision )
{
// add 1 point to CurrentScore when item5 hits item 1
if( theCollision.gameObject == item1 )
{
CurrentScore++;
}
// add 2 points to CurrentScore when item5 hits item 2
if( theCollision.gameObject == item2 )
{
CurrentScore += 2;
}
// add 3 points to CurrentScore when item5 hits item 3
if( theCollision.gameObject == item3 )
{
CurrentScore += 3;
}
// subtract 1 point from CurrentScore when item5 hits item 4
if( theCollision.gameObject == item4)
{
CurrentScore--;
}
}
I was trying to set up the idea that if object 5 collided with any of the other 4 objects points are gained or lost.
When it was first set up that var item1 = GameObject; it won’t even allow me to designate in the inspector which was object 1 etc.
Items 1-4 are prefabs, btw. Anyone out there tonight to help?
change scorineMe() to Update() and you can actually delete var test5 : gameObject; I forgot to delete the first time around. Also, the gameObject you’re attaching to have colliders right? An alterntive way to see if you collided with something would be
function OnCollisionEnter( collision : Collision )
{
if( collision.gameObject.CompareTag("anObjectWithThisTag") )
{
// we collided!
}
}
and give the objects u want to collide with a tag, in which you check for ( CompareTag( “the tag” ) )
I messed with that function name. Oops. Fixed that. But I still get a “NullReferenceException: Object reference not set to an instance of an object” error that I think relates to the GUIText object I’m using for scoring.
The gameobject I’m attaching it to has a box collider yes. As do two of it’s children objects because they protrude from the parent body (posts).
Verbatim the code that is attached to object5 right now reads …
var scoreText;
var item1 : GameObject;
var item2 : GameObject;
var item3 : GameObject;
var item4 : GameObject;
// ths starting score
var CurrentScore = 0;
function Update ()
/* This is here to set the GUIText on the screen where you're displaying the score to the variable that holds the score*/
{
scoreText.text = CurrentScore;
}
function OnCollisionEnter( theCollision : Collision )
{
// add 1 point to CurrentScore when item5 hits item 1
if( theCollision.gameObject == item1 )
{
CurrentScore++;
}
// add 2 points to CurrentScore when item5 hits item 2
if( theCollision.gameObject == item2 )
{
CurrentScore += 2;
}
// add 3 points to CurrentScore when item5 hits item 3
if( theCollision.gameObject == item3 )
{
CurrentScore += 3;
}
// subtract 1 point from CurrentScore when item5 hits item 4
if( theCollision.gameObject == item4)
{
CurrentScore--;
}
}
But the gameobject I’m attaching it to already has a function update script attached to it to move is that reads …
function Update () {
/* Apply translation along the x axis of the object */
transform.Translate (Input.GetAxis ("Vertical") * 3.5 * Time.deltaTime, 0, 0);
/* Apply translation along the z axis of the object */
transform.Rotate (0, Input.GetAxis ("Horizontal") * 90 * Time.deltaTime, 0);
}
And with the two function updates the screen jerks around weirdly. Sorry for the misery … art classes didn’t prepare me to be much of a coder.
EDIT LATER – Hang on, does the “scoreText.text” actually need to have the real name of the GUIText object that has the score (I was just falling asleep when I thouhgt of this)??? DisplayScore.text???
is the problem for me. It doesn’t in any way shape or form relate to a specific GUIText object. How do you give that the ability to be linked in the Inspector to a specific GUIText object in the game scene?
I’ve tried every combo such as scoreText : GUItext; etc but no joy. I’m looking through stuff … but no luck yet.
In additon to the above full script above I’ve created this one because it’s cleaner … and yet still no wayto let the DisplayScore GUIText object know it’s the one the recive/display the score …
Anyone tell me what I’m doing wrong and why I can’t get the DisplaySCore GUIText object in my scene to be the recipient of the CurrentScore variable???
Doing that should expose it via the Unity UI and allow you to drag-assign a GUIText object as its value. Watch the capitalization though, errors (like “GUItext”) will prevent it from appearing in the inspector.
The problem is that you’re updating CurrentScore, but that’s only changing the variable…you haven’t told the GUIText object what to display. So just put "DisplayScore.text = CurrentScore; " after every time you update CurrentScore.
Oops, hang on…you need that to be “DisplayScore.text = CurrentScore.ToString()” I believe.
And to what object is the above script attached? Based on your code it would have to be attached to the GUIText object you want to display the score, otherwise it will fail.
Hey, Glad you figured this one out! Sorry I couldn’t be more specific (my mom was telling me to go to bed because I had school then a baseball game an hour and a half away tomorrow, and still had an hour of homework left to do >.<)