When Time.timeScale go from 1 to 0 Unity destroy a gameobject
I've got this code
 using UnityEngine;
 using System.Collections;
 
 public class BallClick : MonoBehaviour
 {
     //Camera
     public Camera MainCam;
 
     //SpriteRenderer
     private SpriteRenderer spriterenderer;
 
     //RayCastHit2D
     private RaycastHit2D HitInfo;
 
     //Transform
     private Transform RayCastTrans;
 
     void Start(){
 
         spriterenderer = GetComponent<SpriteRenderer> ();
 
         spriterenderer.enabled = true;
 
     }
 
     void Update ()
     {
         
         if (Input.GetMouseButtonDown (0)) {
 
                 DisableSpriteRenderer ();
 
         }
     }
 
     public void DisableSpriteRenderer(){
 
         if (Physics2D.Raycast (MainCam.transform.position, new Vector2 (Input.mousePosition.x, Input.mousePosition.y), 100)) {
 
             HitInfo = Physics2D.Raycast (MainCam.transform.position, new Vector2 (Input.mousePosition.x, Input.mousePosition.y));
 
             RayCastTrans = HitInfo.transform;
 
             if(RayCastTrans.position == this.gameObject.transform.position){
 
                 spriterenderer = GetComponent<SpriteRenderer> ();
 
                 spriterenderer.enabled = false;
 
             }
 
         }
 
     }
 
 }
 
               When i click a button that cange Time.timeScale from 1 to 0 Unity destroy an object.
Please help me and thanks to all in advance!
Where do you set timeScale to 0 ? What is the object that gets destroyed ? How is this script related to the problem ?
I set Time.timeScale in another script, but i think that the problem is in this script because if i disable this script the problem doesn't exist.
The only script when I have the Destroy() function is this
 using UnityEngine;
 using System.Collections;
 public class GroundBall : $$anonymous$$onoBehaviour {
 
     //GameObject
     public GameObject ground;
 
     //Vector3
     public Vector3 target;
 
     //Quaternion
     public Quaternion rotation;
 
     //Color
     public Color red;
 
     public Color blue;
 
     public Color green;
 
     //Number
     int random;
 
     public float range;
 
     //Sprite Renderers
     public SpriteRenderer spriterender;
 
     void Start () {
 
         spriterender.enabled = true;
 
         random = Random.Range (1, 4);
 
         if(random == 1){
 
             spriterender.color = red;
 
         }
 
         if(random == 2){
 
             spriterender.color = blue;
 
         }
 
         if(random == 3){
 
             spriterender.color = green;
 
         }
 
     }
 
     //On collision
     void OnCollisionEnter2D (Collision2D coll) {
         
         if (coll.gameObject == ground && this.gameObject.transform.position.x > -9 && this.gameObject.transform.position.x < 11) {
 
             Instantiate (this.gameObject, target, rotation);
 
         }
 
         if (this.gameObject.transform.position.x > 17) {
 
             Destroy (this.gameObject);
 
         }
 
     
 
     }
         
 }
                 Are you sure it's setting the timescale that destroys something? Have you tried just commenting out the 1 line that changes timeScale and seeing if this still happens?
In the attached script you disable a SpriteRenderer. Are you sure the object gets destroyed and not just hidden?
What is the object that gets destroyed? What scripts are attached to it?
At least this looks wrong
 HitInfo = Physics2D.Raycast ($$anonymous$$ainCam.transform.position, new Vector2 (Input.mousePosition.x, Input.mousePosition.y));
 
                  You are raycasting from camera world coordinates to mouse cursor pixel coordinates. So if you click the bottom left corner, you always cast to 0,0 no matter where the camera is.
Use screenpointToRay to make a straight ray from camera to world You'll have to modify the example for 2D.
Answer by Ciceri · May 16, 2016 at 07:49 PM
I resolved this problem with, MonoBeaviour.OnMouseDown.
Thank to all, good night
Your answer
 
             Follow this Question
Related Questions
Raycast 2D detecting multiple objects in different positions 0 Answers
Detect and destroy a 2D collider if I click with mouse on it 1 Answer
Is there a mesh collider that detects transparency? 0 Answers
Find all locations where a ray/ raycast insects an object 0 Answers
Raycasting not detecting collider 0 Answers