- Home /
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>();
}
}
}
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.
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.
Answer by Loius · Apr 17, 2013 at 09:30 PM
Uh, use Start instead of Update? xD
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.
That's still no reason to put that code in Update. You should only add them once, when you need to, not every frame.
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.
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.
I'm adding it in the ChunkGenerator now. I can't believe I didn't think of that! Thanks!
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!
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
Follow this Question
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