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
1
Question by Olgo · Mar 12, 2013 at 02:42 AM · gameobjectmultipleshare

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;
     }
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 Chronos-L · Mar 12, 2013 at 04:17 AM 0
Share

Gave you a upvote for a hilarious question title that grabs my attention.

avatar image Olgo · Mar 12, 2013 at 01:52 PM 0
Share

haha thanks Chronos. gotta draw attention to my plea somehow.

1 Reply

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

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"?

Comment
Add comment · Show 10 · 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 Olgo · Mar 12, 2013 at 01:58 PM 0
Share

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.

avatar image Olgo · Mar 12, 2013 at 04:39 PM 0
Share

I should add that each of the trees are children of 1 stump. That's good info to know.

avatar image Chronos-L · Mar 13, 2013 at 12:44 AM 0
Share
  • 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.

avatar image Chronos-L · Mar 13, 2013 at 12:49 AM 0
Share

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.

avatar image Chronos-L · Mar 13, 2013 at 01:39 AM 0
Share

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:

alt text

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,

alt text

So, all the towers are correctly built. Just that they are all built in the same location.

screenshot.12.png (34.5 kB)
screenshot.13.png (17.4 kB)
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

11 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

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


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