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 roxasguy · Apr 17, 2013 at 08:50 PM · componentsavingsingleonly

Add only ONE component at runtime?

The meshes in my game are generated at runtime, and I need to add scripts to them when they're generated. I know how to do this, BUT, I need only ONE of each script to be attached! I don't know how...It's probably SO simple...Forgive me....But, Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class GetMapChildren : MonoBehaviour {
     
     public GameObject Map = GameObject.Find("Map");
     public ArrayList MapChidren;
 
     //Attach this script to the, "Map", GameObject!!!
     //This script gathers all of the Children of GameObject, "Map", and gives them the components required to save their data.
     void Update(){
         foreach(Transform child in transform){
             child.gameObject.AddComponent<StoreMesh>();
             child.gameObject.AddComponent<StoreInformation>();
             child.gameObject.AddComponent<StoreMaterials>();
         }
     }
         
 }
Comment
Add comment · Show 2
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 hoy_smallfry · Apr 17, 2013 at 09:38 PM 0
Share

I'm not quite understanding. You want one script for the entire map? or one of each script for each child in the map?

If you are attempting the latter, your problem is that you are doing this in the Update() function, which will add new game objects every frame.

Ins$$anonymous$$d of adding the components in the Update() function, try adding it in the Start() function ins$$anonymous$$d. This will make sure it only happens once.

avatar image roxasguy · Apr 17, 2013 at 10:43 PM 0
Share

Well, my game is infact a $$anonymous$$inecraft Clone. The world is split into chunks, which are generated under the, "$$anonymous$$ap", GameObject. They are generated when the player moves a certain distance away from where the existent ones are. It must be in Update() because new chunks are almost constantly being generated. I want the script above to constantly be finding all of $$anonymous$$ap's children, and assigning these three components to them, and if it finds a GameObject with these components already assigned, then it skips them, and doesn't assign anything.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Loius · Apr 17, 2013 at 09:30 PM

Uh, use Start instead of Update? xD

Comment
Add comment · Show 6 · 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 roxasguy · Apr 17, 2013 at 10:35 PM 0
Share

It needs to be at update, because the meshes are generated constantly. It's a $$anonymous$$inecraft Clone. Chunks are generated when the player moves around.

avatar image Eric5h5 · Apr 17, 2013 at 11:30 PM 0
Share

That's still no reason to put that code in Update. You should only add them once, when you need to, not every frame.

avatar image roxasguy · Apr 17, 2013 at 11:36 PM 0
Share

I don't know when the player is going to move. Therefore I don't know when the chunks will spawn. I put it in Update() so It's always looking for the chunks. In the editor, the $$anonymous$$ap object has no children. When the game runs, it generates a couple chunks, then waits until the player moves away from them, then generates more. Like I said, I don't know when they're going to spawn, so I have it update every frame. I'll try Start() again, and add a few modifications to the code that makes the chunks. Then I'll test.

avatar image Eric5h5 · Apr 17, 2013 at 11:39 PM 1
Share

No, you really don't need to, and should not, add those components every frame. Just add them when a chunk is generated. It doesn't matter at all whether you know what the player will do, since you certainly know when you've created a new chunk.

avatar image roxasguy · Apr 18, 2013 at 12:00 AM 0
Share

I'm adding it in the ChunkGenerator now. I can't believe I didn't think of that! Thanks!

Show more comments
avatar image
1

Answer by roxasguy · Apr 22, 2013 at 03:19 AM

I managed to solve the problem. I made the chunks add the component whenever the chunksSpawn function is called. Therefore decreasing lag, and making me realize how stupid I am to not have done that in the first place! Thanks!

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 hoy_smallfry · Apr 22, 2013 at 06:48 PM 1
Share

You should select this as an answer or close the question. Either way, good job on finding a solution that works for you.

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

14 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

Related Questions

Accessing Functions from other Scripts 1 Answer

using yield in components 2 Answers

error CS1023: An embedded statement may not be declaration or labeled statement 1 Answer

PlayerPrefs not saving 1 Answer

2D Animation does not start 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