• 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 PeterWendig · Jan 23, 2016 at 01:27 AM · instantiatefindgameobjectswithtaggamobject

Instantiating objects to create an infinite level.

I have a main object that has a North, West, South and East object as c$$anonymous$$ldren, ie the name of each object is N,W,S,E. I want the main object to be instantiated and put right in front of me starting with the respective end(when I am facing and going north and I reach the North-object, the main object should be instantiated so I enter South of the just instanced main object etc.). Also, it should remove all instantiated main objects being distant more than 4 instances of the main object of the player.

My code is t$$anonymous$$s and the problems are that rooms are copied randomly. Going back creates new instances in the same place or even different places, sometimes I get null exceptions for Destroy.

What to change:

 void OnTriggerEnter(Collider other)
     {
         //N:Z=1
         //O:X=1
         //W:X=-1
         //S:Z=-1
         if (other.gameObject.name == "N" && transform.forward.z > 0.8f)
         {
             Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z + 8), Quaternion.identity);
         }
         if (other.gameObject.name == "S" && transform.forward.z < -0.8f)
         {
              Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z - 6), Quaternion.identity);
         }
         if (other.gameObject.name == "W" && transform.forward.x < -0.8f)
         {
              Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x - 6, other.gameObject.transform.position.y, other.gameObject.transform.position.z), Quaternion.identity);
         }
         if (other.gameObject.name == "E" && transform.forward.x > 0.8f)
         {
              Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x + 6, other.gameObject.transform.position.y, other.gameObject.transform.position.z), Quaternion.identity);
         }
         Raeume = GameObject.FindGameObjectsWithTag("Raum").ToList();
         if (Raeume.Count > 3 && Raeume.First() != null)
             Destroy(Raeume.First());
         //Debug.Log(other.gameObject.name);
     }
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 cjdev · Jan 23, 2016 at 03:35 AM

At a guess I'd say it's because your box colliders are triggering more than once on the edges. You'll get double copies of t$$anonymous$$ngs and have leftovers where you don't expect. Also, the null reference exception would be because Destroy doesn't work until the end of the frame but since it was called on the same GameObject in those two OnTriggerEnters one of them is left with null to destroy. I'd recommend keeping local reference variables for your current directional GameObjects and comparing that way. Alternatively, you could use a bool to check if the OnTriggerEnter method has been triggered recently.

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trouble with deleting duplicate MusicManager 1 Answer

Can't figure out how to access an object after creation.,Can't figure out how to access a given instance of the object on mouse click. 1 Answer

finding clones and destroying them doesn't work 0 Answers

Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers

GameObject is not reading the rigidbody2D component of instantiated prefab,GameObject is not reading the Rigidbody2D component from instantiated prefab 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