Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by SEDBC3 · Jun 13, 2013 at 05:26 PM · gameobjectdestroyvariablescollisions

How can I set the order of destruction of game objects?

I am trying to guide a player along a path with stationary game objects leading his way (like following a trail of breadcrumbs). I don't want the player to be able to destroy, for example, the 3rd guiding point object if he hasn't already destroyed the 2nd. I wrote this code which I was sure would work, but instead my player just collides with the object, even if the one assigned in the variable has been destroyed. The code is attached to each object along the path, with the variable 'previous' on each object assigned to the object before it. I'm pretty new to coding so if anyone has any suggestions or code that I could look at it would be much appreciated. Thanks!

 public GameObject previous;
 void OnCollisionEnter () {
       if(previous = null) {
             Destroy (gameObject);
             }
                             
 }
Comment
Add comment
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

· Add your reply
  • Sort: 
avatar image
0

Answer by Coderdood · Jun 13, 2013 at 06:52 PM

Try this:

 public GameObject previous;
 void OnCollisionEnter () 
 {
       if(previous == null) 
       {
             Destroy (gameObject);
       }
 }

More information from this article. Quoted because the rest of the article could be confusing. Note they talk about javascript but the idea is the same. Using a single equal sign (=) means something different from using two equal signs (==).

(Everything below is not mine - Quoted from this article)


Assignment versus equality

A mistake novel JavaScripters frequently make is confusing the assignment and equality operators = and ==. If you want to compare two values, you should use a double equal sign since that's how JavaScript works. If you accidentally use a single equal sign, you don't compare values but assign a value to a variable.

For example:

 if (x == 3) {
     doSomething();
 }

This is correct: you compare the value of x to 3. If x in fact has the value 3, the doSomething() function is executed.

This is wrong:

 if (x = 3) {
     doSomething();
 }

Now you do not compare x and 3, but instead you assign the value 3 to x. The doSomething() function is always executed, regardless of the original value of x. This can be quite confusing to novel scripters who think they've done it right.

Now why is doSomething() executed? That happens because the assignment operator = also returns a value: the value you just set the variable to. In our example, the x = 3 statement does two things:

It assigns the value 3 to x. It then returns the same value 3 to whichever JavaScript construct asked for it. In this case, it's returned to the if () statement. The if () statement expects to receive a boolean value true or false. If it receives true the statement doSomething() is executed. If it receives false the statement is not executed.

However, now if () receives the value 3, which is a number and not a boolean. That's perfectly OK to JavaScript. If any operator or statement receives a value of the wrong type, JavaScript silently converts the value to the correct type. Chapter 5 of the book treats these situations in detail.

The number 3 is converted to the boolean true and doSomething() is executed. In fact, almost every number is converted to true, and therefore doSomething() will be executed no matter which value you assign to x.

The only exceptions are 0 and the special value NaN (Not a Number). These two are converted to false.

Usually you don't want all this complicated stuff to happen. Usually you just want to compare two values and take action based on whether they're equal or not. That's why you generally use the equality operator == in such cases.

Comment
Add comment · Show 1 · 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 SEDBC3 · Jun 13, 2013 at 09:01 PM 0
Share

Wow that == worked perfectly. Thanks!!

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to destroy a transform's parent object. 1 Answer

Coding Gui Text To Perform After GameObject Destroy? 1 Answer

Getting errors when referencing a variable. 1 Answer

Missing Reference Exception 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