• 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 /
This question was closed Nov 16, 2014 at 05:10 PM by Jeff-Kesselman for the following reason:

Not a bug. Error in programmer expectations

avatar image
Question by minhdaubu2 · Nov 16, 2014 at 05:08 PM · uibuttonclick

Unity 4.6's button Bug?

T$$anonymous$$s is my simple script:

 using UnityEngine;
 using System.Collections;
 
 public class bt : MonoBehaviour {
     public bool mouseOver = false;
     public GameObject cube;
     public Transform tran;
     private GameObject go;
 
     public void Enter()
     {
         mouseOver = true;
         go = Instantiate(cube, tran.position, tran.rotation) as GameObject;
     }
     public void Exit()
     {
         Destroy(go);
         mouseOver = false;
     }
 
     void Update()
     {
         if(mouseOver)
         {
             if(Input.GetButtonUp ("Fire1"))
             {
                 Destroy(go);
             }
             if(Input.GetButtonUp ("Fire2"))
             {
                 Destroy(go);
                 print ("dose it work???");
             }
         }
     }
 }
 

I add two EvenTrigger on my Button (w$$anonymous$$ch the script was attached to). One call Enter() when PointerEnter and one call Exit() when PointerExit.

The problem is when Left click (Fire1) the object is destroyed. But when I press Right Click (Fire2) the object isn't destroyed (the print command still works)......Help me please, thank all.

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Jeff-Kesselman · Nov 16, 2014 at 05:10 PM

There is no bug. Destroy is working exactly as documented.

rtfm http://docs.unity3d.com/ScriptReference/Object.Destroy.html

Comment

People who like this

0 Show 2 · 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
avatar image minhdaubu2 · Nov 17, 2014 at 10:23 AM 0
Share

can't you tell me where is that "programmer expectations"????? You just click "Fire3" which does not contain in that script it would give the same result (that new object have instantiate)......

avatar image Ben-BearFish · Jul 01, 2015 at 06:50 PM 0
Share

@minhdaubu2, Once Destroy is called the gameobject "go" is destroyed and deleted from memory. So when you left click, Input.GetButtonUp ("Fire1") is called which deletes "go" from memory. Now when you right click, Input.GetButtonUp ("Fire2") is called. When you try to Destroy "go" the second time Unity cannot destroy the gameobject "go", because it is already destroyed and not in memory. So the call to Destroy the second time does nothing.

So you get this:

 if(Input.GetButtonUp ("Fire1"))
 {
    //"go" is destroyed and deleted from memory. "go" is now null.
    Destroy(go);
 }
 if(Input.GetButtonUp ("Fire2"))
 {
    //"go" does not exist anymore. So Destroy(go) equals  
    //Destroy(nothing)
    Destroy(go);
    print ("dose it work???");
 }

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

On Button Click Enable - Disable GameObject Through Inspector 2 Answers

Button is not being clicked. 1 Answer

AddListener not working c# 0 Answers

UI Button: one action after other with only one click 1 Answer

Two buttons behaving entirely differently (despite similar logic in code)? 1 Answer


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