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 vincentamato · Dec 09, 2014 at 02:28 AM · gameobjectinstantiatewallsinstantiate-game-object

Trash Collection for Instantiated Objects

Hi. I know I recently posted a question similar to this, but please ignore that one. It did not provide sufficient detail. Here is the new version. Anyways, in my game the player is climbing up infinitely instantiated walls. I want to delete the walls that are no longer needed. Here is the code I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class BrickWallTrashCollection : MonoBehaviour {
 
     public GameObject wall;
     public Transform player;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         if(wall.transform.position = Vector3.Distance(player.position.x + 10, player.position.y +10){
            Destroy(wall.gameObject);
         }
     }
 
 
 }

I get an error on the open bracket right after the if statement at the end. It tells me that this was an unexpected symbol. I know there are multiple things wrong with this code so can somebody please point me in the right direction? Thanks!

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
Best Answer

Answer by theLucre · Dec 09, 2014 at 02:37 AM

A few things:

  1. When comparing values, use the == operator, not a single =

  2. You are comparing Vector3 and float values in your if statement, which is not possible.

  3. The wall property you have is already a GameObject, so you don't need to use wall.gameObject.

Instead try this, where you set a distance value in float units, and the wall will be destroyed when closer than that distance in 3D space. Be sure to stop using the wall reference after you destroy it.

 if( Vector3.Distance( wall.transform.position, player.position ) <= SomeDistance )
     Destroy( wall );

Or if you're specifically comparing X only:

 if( wall.transform.position.x  >= player.position.x - 10 || 
     wall.transform.position.x  <= player.position.x + 10 )
     Destroy( wall );
Comment
Add comment · Show 4 · 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 vincentamato · Dec 09, 2014 at 02:43 AM 0
Share

@theLucre Sorry I am a beginner/intermediate coder so would I fill in a point for SomeDistance or what?

avatar image theLucre · Dec 09, 2014 at 03:32 AM 0
Share

Not a Vector point, but a float number. It would be the number of units the player is from the Wall when it would be destroyed.

avatar image vincentamato · Dec 10, 2014 at 02:03 AM 0
Share

@theLucre Hi. I used your first suggestion of code and it works fine but the problem is that it destroys walls that the player has not reached yet. How can I make it only destroy walls under the player?

avatar image vincentamato · Dec 10, 2014 at 02:09 AM 0
Share

@theLucre Sorry with all the questions but how can I make it destroy any game object with the tag "Wall" ins$$anonymous$$d of it only destroying the wall that has been assigned to the wall game object?

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Instantiated objects always at position 0,0,0 0 Answers

Trash Collection For Instantiated Game Object 1 Answer

Instantiate gameobject 2 Answers

Multiplayer game, object instantiated is being added for every single player 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