- Home /
Collision with cube isn't working when Instantiate clone it
Hi guys, my first question on here, I' trying to build a game where a cylinder rotates around and when it complete a rotation it adds 1 to the score. I tried doing this by adding a cube into the scene with the following script, so that when the cylinder collides with the cube it adds one to the score.
var scoreText: GUIText;
private var score : int;
var counter: GameObject;
function Start(){
//counter2 = GameObject.Find("Score");
score = 0;
UpdateScore();
}
// Destroy everything that enters the trigger
function OnTriggerEnter ( Collider ) {
if(gameObject.tag == "Pick")
//for (var x = 0; x > 1; x++){
collider.enabled = true;
Instantiate(counter, Vector3(42.50991, 5.041063, 34.19414), Quaternion.identity);
Destroy(counter);
score = score + 1;
UpdateScore();
// }
}
function UpdateScore (){
scoreText.text ="Count: " + score;
}
The problem I'm having is that when the cube is instantiated it loses it collision and stops adding to the score. Any help would be appreciate, been trying to do this for a couple of days.
Answer by Jan_Julius · Sep 10, 2014 at 01:36 PM
Have you put your cube in a prefab? does the prefab have all the components it's supposed to have? (Collider and your script) and do you instantiate the right prefab if you have multiple ones?
Thanks for answering. I'm not using the cube as a prefab because I couldn't get the prefab to load up GUIText when it was respawn, after it was destroyed, so that the next it was destroy it would update the score GUIText , I spent 3 hours yesterday trying to get that to work and couldn't, if you think that the way I should go could you tell me how to load a prefab with GUIText after it respawn so that it can update score.
Oh I see now, this is attached to the cube, try to seperate the score into another script (try making an empty gameobject attaching the script to there) and access the script when a cube gets destroyed this means you also handle the gui in the script attached to the empty gameobject. An easy way to do this is for example if your script is called "score.cs" and you make a score variable and make that static ( allowing it to be accessed from anywhere in the game from any scene) you can simply type
Score.score++;
whenever a cube gets destroyed in the script where the cube gets handled.
Hope this helps. :)
I created the score script
var scoreText: GUIText; static var score : int;
function Start(){
score = 0;
UpdateScore();
}
function Update(){
score = score+1;
UpdateScore();
}
function UpdateScore (){
scoreText.text ="" +score;
}
Am this is my Destroy code which does destroy the cube.
var counter : GameObject;
// Destroy everything that enters the trigger
function OnTriggerEnter (Collider ) {
if(gameObject.tag == "Pick")
Destroy(counter);
Score.score++;
}
Something wrong through that is when the cube is destroyed it doesn't update the score. An the Score script just keep on counting until infinite. $$anonymous$$anage to get the spawn to work, just got to get the three scripts working together.
Answer by programmrzinc · Sep 13, 2014 at 02:39 AM
Instanitate outside the TriggerEnter ( How can a GO start a trigger if it was never created? ) use a temporary variable to hold your newly created GO ( var newGO = Instantiate(...);
)
Answer by knowles2 · Sep 21, 2014 at 12:30 AM
Well seriously it actually came in a dream that I was being way to complicated. I didn't need to instantiate cube at all or destroy anything or spawn spawn anything.
All I needed to do to achieve what I wanted was just add the score script to a cube and then setting cube collision to trigger and counted the number of times my object spun around.
Your answer
Follow this Question
Related Questions
Score count increase on hit 2 Answers
Score updating after collision 1 Answer
Scoring (i'm stuck) 3 Answers
how to keep track of bricks broken 1 Answer
Score multiplier 1 Answer