Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ciceri · May 10, 2016 at 08:37 PM · collider2draycastinggameobjects

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!

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · May 10, 2016 at 10:04 PM 0
Share

Where do you set timeScale to 0 ? What is the object that gets destroyed ? How is this script related to the problem ?

avatar image Ciceri · May 11, 2016 at 06:18 AM 0
Share

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.

avatar image Ciceri · May 11, 2016 at 06:46 PM 0
Share

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);
 
         }
 
     
 
     }
         
 }




avatar image Ciceri · May 12, 2016 at 01:42 PM 0
Share

Can someone answer me?

Thank to all

avatar image NoseKills · May 13, 2016 at 06:40 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ciceri · May 16, 2016 at 07:49 PM

I resolved this problem with, MonoBeaviour.OnMouseDown.

Thank to all, good night

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges