I feel like I’m either using the wrong collision code, or that there’s an easier way to execute this scoring system. I made a quick 3D torus loop (for a simple basketball rim) in 3ds Max, imported it into Unity, gave it a mesh collider, then duplicated it, and named them Hoop2 and Hoop3, with the idea that Hoop2 is scaled larger and will give the player 2 points, whereas Hoop3 is scaled smaller and will give the player 3 points for making the better shot. For now the player just has to physically hit the rim to score, but that’s okay. Then I made a cube and called it Bullet, and then made it a prefab object.
Here’s the code for Shooting.js:
var preBullet:Transform;
var shootForce:float;
function Update ()
{
if(Input.GetButtonDown("Jump"))
{
var instanceBullet = Instantiate(preBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
Here’s the code for my Scoring.js:
var BasketballPoints:int = 0;
var Scoreboard:GUIText; // Screen GUI Score Counter
function OnCollisionEnter(theCollision:Collision){
if(theCollision.gameObject.name == "Hoop2"){
BasketballPoints += 2;
//GetComponent(GUIText).text = "Score: " + BasketballPoints;
}
else if(theCollision.gameObject.name == "Hoop3"){
BasketballPoints += 3;
//GetComponent(GUIText).text = "Score: " + BasketballPoints;
}
Scoreboard.text = "Score: " + BasketballPoints;
}
I put the Shooting script on the Main Camera and assigned the Bullet object to Shooting’s preBullet transform slot in the Inspector viewport (am I saying that right?), and then added the Scoring script (and rigidbody) on the Bullet object, and then made a new GUIText object called ScoreText and dragged that into Scoring’s GUIText selector slot in Bullet’s Inspector menu.
It runs, but here are the problems.
-
Because the collision is on the bullet, every time you hit one of the hoops, the score resets to 0 and then gives you 2 or 3 points. I tried doing the Scoring.js through the bullet prefab object, but when I do that it won’t let me drag a GUIText onto its selector, so it won’t work. Instead of asking when a current object (that a script is attached to) hits some other object, is there a code that lets me ask if object 1 collides with object 2, that I can use to put on my camera (which is neither object 1 or 2) so it won’t reset the score each shot? What’s the best way to solve this?
-
How can I make it that one bullet projectile can only score points once no matter what, so that bobbling/bouncing shots don’t trigger multiple scores (before resetting one shot later). Would I make a boolean variable and then only accept a score if it’s turned on, and then turn it off?
-
Also, if I was stubborn and wanted to do it another way, and I had a variable in the Shooting script, is it possible to make a GUIText object in the scene hierarchy and give it a new script that can access the public variables that were created in Shooting.js? How could I get a variable or string from inside one script and display it on screen through a separate script?
-
In the interest of learning how exactly Unity works internally, when I create a GUIText through code (like my second line of Scoring.js), why do I need to make a GUIText object in the hierarchy and link to it in the first place? Obviously it works, but why can’t/doesn’t it just make it on its own through code?
Many thanks. I’m new to object oriented programming, so I feel like I’m getting close to learning the knack for how different scripts can talk and interact with each other. I appreciate your help greatly!