- Home /
Instantiate not working
I do not understand how this function works. The script is without error but I would like to make this script work. It does not create the object when it reaches the limit
var ScoreS : ScoreScript;
var Activate : boolean;
var Spawner : GameObject;
var SpawnObj : GameObject;
function Update(){
var Score : int;
for(var i:int =0; i < Input.touches.Length; i++){
var touch:Touch=Input.touches[i];
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
var hit:RaycastHit =new RaycastHit ();
if(Physics.Raycast(ray,hit,1000)){
if(hit.collider.gameObject ==this.gameObject){
switch(touch.phase){
case TouchPhase.Began:
Score += 1;
break;
}
}
}
}
if(Score == 10){
Activate = true;
}
if(Activate == true){
Instantiate(SpawnObj,Spawner.transform.position,Spawner.transform.rotation);
Score = 0;
Activate = false;
}
}
var ScoreS : ScoreScript;
var Activate : boolean;
var Spawner : GameObject;
var SpawnObj : GameObject;
function Update(){
var Score : int;
for(var i:int =0; i < Input.touches.Length; i++){
var touch:Touch=Input.touches[i];
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
var hit:RaycastHit =new RaycastHit ();
if(Physics.Raycast(ray,hit,1000)){
if(hit.collider.gameObject ==this.gameObject){
switch(touch.phase){
case TouchPhase.Began:
Score += 1;
break;
}
}
}
}
if(Score == 10){
Activate = true;
}
if(Activate == true){
Instantiate(SpawnObj,Spawner.transform.position,Spawner.transform.rotation);
Score = 0;
Activate = false;
}
}
Also its always better to edit the question when just the code formatting is needed in the question. It helps keep things neat. I've done that for you this time. You can do that from next time for your questions. :-)
Just check if(Activate == true) loops works at all or not. Also check if Score is getting incremented at all.
When you have no errors in your code then you might want to use Debug.Log() to check if certain conditions in your are working as expected or not.
Answer by techshaman · Jun 20, 2014 at 03:13 PM
I imagine your "if(Activate == true)" block is never being entered.
The problem is that you define the Score variable inside the update loop...meaning that each frame you are making a new integer (being set to 0). You are tracking the number of touches in that frame. Unless you are touching whatever gameobject leads to your 'score +=1' enough times in a single frame, you would never hit to gameobject instantiate code.
EDIT: As a side note, you should not be creating gameobjects at runtime. This is bad unity practice and will result in unavoidable, visible performance hiccups while you play the game. This is especially bad to do in an Update() loop. Instead, you should create all the gameobjects you will use at the beginning (say when a scene loads). Maintain references to these gameobjects and manipulate them as you need them.
Your answer
Follow this Question
Related Questions
TouchPhase not firing correctly - Android 3 Answers
detect Swipe in four directions (android) 7 Answers
Make an object move in the direction of touch 0 Answers
Move horizontally on touch 0 Answers