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
0
Question by isaacnorman · Dec 04, 2017 at 02:41 PM · instantiatestoring

Best way to instantiate a prefab and store values with it?

I'm trying to make a scene with a set of objects created from prefabs. Each needs to have an x/y coord saved with it - not the actual transform position, but game coords used for procedurally generating their contents.

I have a 'SpawnSectors' script in my GameController object. In the start function I loop through instantiating the prefab, retrieving a script from the new object and call SetCoords. The SetCoords funtion just stores the values in private class members.

 for (int x = -5; x < 5; x++)
         {
             for (int y = -5; y < 5; y++)
             {
                 Vector3 pos = new Vector3(sectorSpawnOrigin.position.x + (x*100), sectorSpawnOrigin.position.y + (y*100), sectorSpawnOrigin.position.z);
                 GameObject sector = Instantiate(sectorPrefab, pos, sectorSpawnOrigin.rotation);
                 sector.transform.parent = sectorCanvasTransform;
                 PerlinToImageColour p2c = sector.GetComponent<PerlinToImageColour>();
                 p2c.SetCoords(x, y);
             }
         }

I notice from some debugging that all this happens before the new objects start function runs. It's in start that it uses the coords, however when this happens the coords have been reset to 0.

Does whatever calls the start function intentionally reset all fields to zero? How can I pass a value to a newly created object?

I'm very new to Unity, so am I going about this all the wrong way?

Thanks

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 isaacnorman · Dec 04, 2017 at 04:23 PM 0
Share

Thinking about it, could there be some even I can listen for - so a script creates a bunch of objects then is able to register for when they're all 'ready' for further calls?

All seems a bit messy, like I'm going about things backwards - maybe because i'm trying to put the intelligence in the prefab rather than the thing creating the prefab. That does seem the more unity way though.

1 Reply

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

Answer by fafase · Dec 04, 2017 at 05:06 PM

Create a script and attach it to the prefab:

 public class DataContainer:MonoBehaviour
 {
      int a, b;
      public void SetValues(int a, int b)
      {
             this.a = a;
             this.b = b;
      }
      public int GetA(){ return this.a; }
      public int GetB(){ return this.b; }
 }

On creation, access and set the values:

 GameObject obj = (GameObject)Instantiate(prefab);
 obj.GetComponent<DataContainer>().SetValues(i, j);

Another solution is to use a dictionary to store the value:

 public struct DataStruct
 {
     public int a = 0;
     public int b = 0;
     public DataStruct(int a, int b)
     {
          this.a= a; this.b = b;
     }
 } 
 Dictionary<GameObject,DataStruct>dict = new Dictionary<GameObject, DataStruct>();

then in the loop when creating the object:

dict.Add((GameObject)Instantiate(prefab), new DataStruct(i, j));

Comment
Add comment · Show 5 · 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 isaacnorman · Dec 04, 2017 at 07:26 PM 0
Share

Thanks for the reply.

Unless I've missed something though, isn't the first solution the same as $$anonymous$$e?

Instantiate object, get script on new object, call set function.

The problem I'm having is that after I call the set function and sure the values as class members the start function is called and the members reset.

avatar image ShadyProductions isaacnorman · Dec 04, 2017 at 07:40 PM 0
Share

Then don't reset the fields in start? :) They can't just reset unless you specifically reset them yourself or not initialize them at all.

avatar image isaacnorman ShadyProductions · Dec 04, 2017 at 07:50 PM 0
Share

No I'm saying they're already reset by something when start is called. It's like whatever calls start first zeros the object.

I've done some googling and found at least one other person who sees the same thing. $$anonymous$$aybe I can make a simple project that shows it and share.

Show more comments

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

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

Related Questions

Checking if object intersects? 1 Answer

Instantiating prefabs, when there is nothing 2 Answers

Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer

continuous shooting 1 Answer

[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 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