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 DarkSlash · Feb 19, 2015 at 09:36 PM · transformupdatetransform.position

Update instantiates more than 1 object

If the object has reach certain position.x, I want that that object clone itself, but in another position. I wrote this code in the Update function:

 if(transform.localPosition.x <= -6.26f && canBuild == true)
         {
             canBuild = false;
             Instantiate(gameObject, new Vector3(transform.position.x + 15.65f, transform.position.y, transform.position.z), Quaternion.identity);
         }

I use localPosition because the object, in Start() is parent of the camera.

The code seems to run well, but sometimes, it enters twice in the IF sentence, so 2 objects are created instead of one.

Why is happening this? How can I fix it?

Comment
Add comment · Show 6
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 Bonfire-Boy · Feb 19, 2015 at 10:12 PM 0
Share

Hard to tell just from that. But a few questions spring to $$anonymous$$d...

Does anything ever set canBuild to true? If not then it seems unlikely that the if clause is being entered twice on the same object. By the way, it's not static is it?

How many objects have this component?

And is it possible that the clone is cloning itself?

Logging, including a unique identifier for the cloning object as well as its position, would probably clear it up.

avatar image DarkSlash · Feb 19, 2015 at 10:17 PM 0
Share

canBuild is initialized as true. It enters in the IF so is not the problem!

Just one kind of object. The floor object. That clones itself when the camera moves right.

Yes, the clone clones itself when it reach the position.x desired.

avatar image Bonfire-Boy · Feb 19, 2015 at 10:18 PM 0
Share

So how do you know that the second object isn't a clone of the clone?

avatar image melkorinos · Feb 20, 2015 at 12:01 AM 0
Share

Try na$$anonymous$$g them with a counter will sort things out

avatar image Bonfire-Boy · Feb 20, 2015 at 12:34 AM 0
Share

Could just use the instance id. Something like this...

 GameObject clone = Instantiate(blah blah) as GameObject;
 Debug.Log(this.GetInstanceID()+" just cloned "+clone.GetInstanceID());

Then you'll know if the same object is cloning multiple times, if it's a chain with the first clone cloning another, or something else.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nymisu · Feb 20, 2015 at 09:53 AM

Depends on how many you want to spawn. If you want to control exact amounts, you could make either a list or a singular gameobject, as follows:

 list<GameObject> horribleMonsters = new list<GameObject>();
 //or
 GameObject terribleBeast;
 
 
 if(transform.localPosition.x <= -6.26f && canBuild == true)
 {
 canBuild = false;
 if(terribleBeast == null)
 {
 terribleBeast = Instantiate(gameObject, new Vector3(transform.position.x + 15.65f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
 }
 //or
 if(!horribleMonsters.Exists(s => s.identifyingcharacteristicsuchasname("Steve"))
 {
 GameObject x = Instantiate(gameObject, new Vector3(transform.position.x + 15.65f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
 horribleMonsters.Add(x);
 }
 }

Keep in mind updateloop can fire multiple times a frame if your game hitches. Try setting timescale to 0 for instance, lots and lots of updateloops within singular frame.

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

Answer by DarkSlash · Mar 06, 2015 at 02:46 PM

Couldn't find the reason, but instead of Instantiate/Destroy I made an Object Pool and solve this issue and also increased the performance!

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

23 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

Related Questions

[2D] Get the position of an object outside the scope 2 Answers

Does it help performance to first check if some transform attribute has changed before setting it? 1 Answer

moving objects with transform position 2 Answers

Why can't I assign transform.position to a Vector3 object? 3 Answers

Why is rotation offset 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