- Home /
How do I get each GameObject to mind its own business?
Hi Guys,
Pretty new to programming here.. I have created a script to generate things that stack. Such as Trees or Towers. My plan is to put 1 base item down, it will stack X amt of mid sections on top of it, and place a topper on it when its done.
So far this works, but only if I have GameObject using the script at a time. If I try to add more GameObjects using the same script, they all just build off of 1 starting point on top of each other!!
How do I get these GameObjects to do their own thing? Have a look at my script. Thanks!
public GameObject[] basePiece;
public GameObject[] midPiece;
public GameObject[] topPiece;
public int _height = 1;
public int maxHeight = 20;
public int minHeight = 1;
public bool isTree = false;
public bool isTower = false;
public float treeTilt = 0.0f;
public GameObject curSpawnPoint;
public GameObject curObject;
void Awake () {
curObject = this.gameObject;
if(isTower)
SpawnTower();
if(isTree)
SpawnTree();
}
public void SpawnTower(){
if ( minHeight < 1 ){
minHeight = 1;
}
if ( maxHeight > 20 ){
maxHeight = 20;
}
_height = Random.Range(minHeight, maxHeight + 1);
curSpawnPoint = GameObject.Find("spawnPoint");
for (int cnt = 0; cnt < _height; cnt++){
GameObject go = Instantiate(midPiece[Random.Range(0, midPiece.Length)],
curSpawnPoint.transform.position,
curSpawnPoint.transform.localRotation
) as GameObject;
go.transform.parent = curSpawnPoint.transform;
curObject = go;
curSpawnPoint = curObject.transform.Find("spawnPoint").gameObject;
}
GameObject goTop = Instantiate(topPiece[Random.Range(0, topPiece.Length)],
curSpawnPoint.transform.position,
curSpawnPoint.transform.localRotation
) as GameObject;
goTop.transform.parent = curSpawnPoint.transform;
}
Gave you a upvote for a hilarious question title that grabs my attention.
haha thanks Chronos. gotta draw attention to my plea somehow.
Answer by Chronos-L · Mar 12, 2013 at 04:06 AM
How sure are you that different gameObject are building on the same tower?
From your code (which I did not run and test yet), you are spawning the tower in one spawn point. (`curSpawnPoint = GameObject.Find("spawnPoint");`)
Is it possible that your code is working, but all the tower are built in the same spot, hence creating the illusion of "gameObject sticking into another gameObject's business"?
I might not have been clear enough, but they are in fact all starting from the same Vector 3. Not that they are building on top of each other in the Y direction. They do all share the same X and Z coordinate starting point.
For example, I found out about my problem when I started using the script to build trees from sectional pieces I made in Blender. I put down 3 base tree pieces at 3 separate locations. Added my script to each of em and hit play. 2 of the stumps have nothing growing on em, 1 of em has 3 trees of random height growing from the same spot.
I had a suspicion this had something to do with static variables? I don't quite understand them but after reading documentation on them, I don't think its what I want. I always use private/public.
I should add that each of the trees are children of 1 stump. That's good info to know.
might not have been clear enough, but they are in fact all starting from the same Vector 3*.
I think I get what you mean clear enough. It is exactly because your tower starts from the same Vector3, then you get this problem. I did not say they build on top of each other, they are building themselves using the same space.
You want to get towers with random height I draw these horizontally, this is what you are expecting:
[ ] tower 1
( ) tower 2
O tower 3
//------ this direction is y-axis -------------->
[ ][ ][ ][ ]
( ) ( ) ( ) ( ) ( ) ( ) ( )
O O O
However, since they start on the same location, what you will get is:
//------ this direction is y-axis -------------->
[(O)][(O)][(O)][( )] ( ) ( ) ( )
Your tower is doing the impossible: sharing the same physical space. I think this is what happens to your code.
You need to fix this part curSpawnPoint = gameObject.Find("spawnPoint");
. You should do some calculation to offset the position of your spawnPoint
gameObject everytime you spawn a new tower.
Fix this one and your code should be okay, I see no other problem with your code.
You know what, since I have some spare time right now, I will set up the scene and run this code to check my hypothesis. Be back soon.
I ran the code. It works fine when there is one towerBuilder object. However, when there's more than one towerBuilder object. Here's what happen:
The script do create 3 tower, but they are all in the same location. (Please note the hierarchy view, the spawnPoint has 3 children)
When I move the towers around,
So, all the towers are correctly built. Just that they are all built in the same location.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make sure a script is not shared? 1 Answer
Is this doable in unity 2 Answers
get all scripts attached to gameobject 2 Answers
how to build & populate an array available to all scripts on all objects? 1 Answer
Help touch button 1 Answer