Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by tasbar · Mar 04, 2010 at 01:21 PM · instantiateloop

Why is there a difference between script and in-game sequence of a script loop?

Within Start() I am instantiating a prefab several times along the z axis . I.e. the z-position is increased for each new instance of the prefab. Strangely the LAST instance is instantiated at the FIRST z position when I start the game.

Details:

In Start() I am running this loop

_createPosition = new Vector3(0, 0, 0);
for (_parcelCounter = 1; _parcelCounter <= 7; _parcelCounter++)
{
  _createPosition = CreateParcel(_createPosition, _parcelCounter);
}

Within the function CreateParcel() the z position is increased, an instance of the prefab is instantiated at this z position and the name of the instance is changed (PrefabInstance.name = "Instance " + parcelCounter;). The function looks (simplified) like this:

Vector3 CreateParcel(Vector3 parcelPosition, int counter) { parcelPosition.z = parcelPosition.z + 5; Instantiate(ParcelPrefab, parcelPosition, Quaternion.identity); ParcelPrefab.name = "Instance " + counter;

return(parcelPosition); }

When I log the data with Debug.Log("Instance " + counter + ": " + createPosition) I get the following output when I run the scene:

Log results: Instance 1: (0.0, 0.0, 5.0) Instance 2: (0.0, 0.0, 10.0) Instance 3: (0.0, 0.0, 15.0) ... Instance 7: (0.0, 0.0, 35.0)

But when I check the instances in the scene hierarchy, the instances don't match this sequence. When I check the position of the different instances with the Inspector it is the following sequence:

Inspector results Instance 1: (0.0, 0.0, 10.0) Instance 2: (0.0, 0.0, 15.0) Instance 3: (0.0, 0.0, 20.0) ... Instance 7: (0.0, 0.0, 5.0)

That is ... (the logged) instance 7 is not at the end of the sequence as it should be and as the log said, but it is in the first position of the sequence. Where does this difference come from? Or is there any silly error in my thoughts/script?

Comment
Add comment · Show 1
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 Lipis · Mar 04, 2010 at 07:24 PM 0
Share

@Duck is right and your problem will be solved automagically. You could also get rid of the return(parcelPosition); if you simply change the parcelPosition.z + 5 to parcelPosition.z + 5 * counter and changing your loop in Start() accordingly, but only if you don't need to know the position of the newly created object.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by duck · Mar 04, 2010 at 05:33 PM

I think your confusion is coming from the fact that you're renaming the prefab each time, rather than renaming the instance of the prefab that was just created.

This means that when you set the name, you're affecting the name of the next instance to be created, rather than the one that was just instantiated!

You're currently not getting a reference to the instance that was just created. The Instantiate function returns a reference to the object it just created, so you need to pipe this into a variable of the correct type.

To fix this, you'd need to change the 2 lines in "CreateParcel" to this:

GameObject instance = (GameObject)Instantiate(ParcelPrefab, parcelPosition, Quaternion.identity);
instance.name = "Instance " + counter;

I'm assuming your prefab variable's type is "GameObject". If it's something else like "Transform", change the types in the code above to suit.

Once you've done this, your mystery should be solved :)

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 tasbar · Mar 12, 2010 at 02:44 PM 0
Share

You were absolutly right. Now after reading it, it seem so obvious. Thank you very much for clearing my mistaken thinking ;).

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

No one has followed this question yet.

Related Questions

Is there a way to call up a IF statement like a function? 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Null Reference Exception on instantiated object's script. 2 Answers

Slow automatic shooting script? 2 Answers

How Instantiate Object Inside of Object and Instantiate Parent Object On Looping. 0 Answers


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