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*;*
  • 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){
    Score = 0;
    Activate = false;

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.

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.