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 uberokeer · Jul 05, 2013 at 05:56 PM · gameobjectrtsfindgameobjectswithtag

RTS Building Creation System Code not working (no errors)

 var Prefab : Transform;
 function OnGUI () {
       if (GUI.Button(Rect(10,10,50,50),"Create")){
             var gos : GameObject[];
             gos = GameObject.FindGameObjectsWithTag("SetObject");
             for (var g in gos){
             if (g.GetComponent.<ControlObject>().isBuild){
             Instantiate(Prefab, g.transform.position, Quaternion.identity);
             }
             }
         }
     }

For some reason this code doesn't do what i want it to do. I don't get any errors, but it doesn't work.

Basically what I'm trying to do is: Check every GameObject within a Array to see if there is a Boolean that is set to true called: 'isBuild' and if there is, then instantiate a prefab on top of them. Alright I'm making a RTS game, what this system is, is a build mode sort of thing. I'm trying to create a building maker, one like on Age of empires for instance. So i'm able to enable build mode, then select a building such as a house and then a grid appears(this is in a whole other script) the grid is 68x68 and if i click on one of the segments of the grid it will set: 'isBuild' true for that segment and then i can hit create and it will check every GameObject(grid segments) to see if: 'isBuild' is true in any of them, thus Instantiating a house(prefab) on top of that object. But yeah that's sort of the plot here. If anyone knows why this code isn't working, then please tell me :) Thanks in advance!

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 iwaldrop · Jul 05, 2013 at 06:25 PM 0
Share

You shouldn't be calling FindGameObjectsWithTag() in an update loop (OnGUI is an update loop of sorts). You should cache the value of it and then iterate over that. You might consider using a Dictionary or Hashtable in order to correlate GameObjects to their Components, because getting all game objects with a certain tag and then all of the ControlObject components potentially twice every frame (OnGUI is called twice per frame) is going to really slow things down.

avatar image uberokeer · Jul 05, 2013 at 06:33 PM 0
Share

uhm no i only do it once. So once i set all the buildings to have the value: 'true' i then hit the create button once. And then it will instantiate buildings on all the ones that are true. so the line:

 gos = GameObject.FindGameObjectsWithTag("SetObject");

Is only called once when i hit the create button.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiloblargh · Jul 05, 2013 at 06:10 PM

I think you just need to say;

 for (var g : GameObject in gos)

or if that doesn't work:

 for (var g : int = 0; g < gos.Length; g++)
     {
     if (gos[g].GetComponent.<ControlObject>().isBuild){...


More than that, you need to learn how to use Debug.Log. That would quickly tell you what is happening and what's not happening and where the error is coming up.

Also- never name a variable starting with a capital letter. Just. Don't.

I'm not sure if UnityEngine has such a thing as a Prefab class, but if it did; calling your variable Prefab instead of prefab would break everything. You should probably come up with a more descriptive name anyway.

Comment
Add comment · Show 3 · 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 uberokeer · Jul 05, 2013 at 06:27 PM 0
Share

Okay what do you mean by

 for (var g : int = 0; g < gos.Length g++)
     {
     if gos[g]...

??? Please more information.

avatar image Kiloblargh · Jul 05, 2013 at 06:37 PM 1
Share

That's a plain old for loop. I missed a ";" the first time. It means check gos[0], then increment and check gos{1], then increment and check gos[2], then increment and check gos[3], keep doing that as many times as there are objects in the array, then stop.

I always do it that way, so I don't know how well for (in ) works in UnityScript; I just recall having some problems with that in the past so I don't use it. It's a shortcut to iterating manually.

avatar image uberokeer · Jul 05, 2013 at 07:12 PM 0
Share

Okay thanks i'll try that :D

avatar image
0

Answer by Noztradamuz · Jul 05, 2013 at 07:00 PM

Try to put this code in a separate function something like

 void SpawnNewObject()
 {
  var gos : GameObject[];
            gos = GameObject.FindGameObjectsWithTag("SetObject");
            for (var g in gos){
            if (g.GetComponent.<ControlObject>().isBuild){
            Instantiate(Prefab, g.transform.position, Quaternion.identity);
 }

and call it from the OnGUI button, but this is going to create an infinite loop if you not change the isBuild variable while you instantiate them.

try this:

     void SpawnNewObject()
     {
      var gos : GameObject[];
                gos = GameObject.FindGameObjectsWithTag("SetObject");
                for (var g in gos){
                if (g.GetComponent<ControlObject>().isBuild){
                var temp = Instantiate(Prefab, g.transform.position, Quaternion.identity);
                temp.GetComponent<ControlObject>().isBuild = false;
     }

and as far as i know you dont need to use a dot before GetComponent<> just use brackets. i'm not very familiar with JS hope this do the job

Comment
Add comment · Show 3 · 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 uberokeer · Jul 05, 2013 at 07:09 PM 0
Share

This will be hard to put together because this code consists of c# and Javascript and i'm only good with Javascript :P

avatar image Noztradamuz · Jul 05, 2013 at 07:14 PM 0
Share

I just take your very code and edited sorry, ignore my "void SpawnNewObject()" metod and make one of your own, just make sure to take the returning gameobject from Instantiate to a new variable and then change the isBuild parameter

avatar image uberokeer · Jul 05, 2013 at 07:30 PM 0
Share

Haha okay i will try this :D

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

17 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

Related Questions

Multiple Cars not working 1 Answer

HELP Find gameObject With tag in another array 1 Answer

Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject' 1 Answer

Moving Platform 5 Answers

Message sender...how to know who send the message 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