Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by RyanGallagher · Mar 07, 2013 at 10:37 PM · c#destroyontriggerenterdoor

How do I make this switch destroy an object?

I'm trying to make a basic maze game, where going on a switch will 'open' a door, basically just destroying it using the OnTriggerEnter command. If the door has already been destroyed, nothing will happen, for which I just put in a Debug.Log line.

The main problem is "error CS0029: Cannot implicitly convert type `bool' to UnityEngine.GameObject'". I know there's probably a very simple solution here, but I just can't seem to get it right. Here's the code.

public class Switch : MonoBehaviour {

 public GameObject door;
 
 void OnTriggerEnter(Collider other) {
     Debug.Log ("Door Open");
     **if (door = true) {**
         Destroy(door);
         Debug.Log ("Door Open");
     }
     
     else{
         Debug.Log("Already did that");
     }
 }

}

Problem line was bolded. Thanks ahead of time.

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 hoy_smallfry · Mar 08, 2013 at 12:47 AM

1) You're trying to assign the boolean value true to door instead of checking if they are the same. There's a distinct difference between = and ==.

The = operator assigns a value, like so: int i = 1;

The == operator compares if two values of the same type are equal, and returns a bool, like so:

 bool b = 1 == 3;

 if(b)
 {
     Debug.Log("they are equal!");
 }
 else
 {
     Debug.Log("they aren't equal!");
 }

The != operator, on the other hand, does the exact opposite - compares if two values of the same type are not equal:

 bool b = 1 != 3;

 if(b)
 {
     Debug.Log("they aren't equal!");
 }

2) C# is a strong-typed language, which means two unrelated types can't become other types like they do in Javascript or Lua, at least not without some coercion. If you are declaring a value as a GameObject, such as door, it will be a GameObject for the entirety of its existence.

That said, you are trying to assign door (a GameObject type) the value true (which is for bool types). This is like shoving a banana into a keyhole and expecting it to fit, let alone work.

The only time you can make the value of one type into the value of another is if you cast from one to the other, implicitly or explicitly. This works only if the types are allowed to, which explains the error you are getting; bool and GameObject can't.

3) GameObject and all other C# classes are reference types, which means the variable is currently referring to something in memory, or its not. If it is not referring to something that exists, then it's value is null.

All that said, your problem line should most probably be replaced with this:

 // if the door exists
 if(door != null)

This is the C# way of checking if the object has been initialized.

Hope that helps!

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to only delete one of two collided objects? 1 Answer

Help with destroying objects on collision 2 Answers

Script for a door that opens and closes help! 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