Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bill9009 · Feb 14, 2017 at 12:07 AM · c#tagselectionforeach

Why can't I use Selection.GetFiltered with GameObjects?

When I look this method up it only says to use a type, but the GameObject type does not work with it. How should I write this instead so unity does not give me the error ('GameObject' is a type, which is not valid in the given context)?

 private void Update()
     {
         foreach(GameObject g in Selection.GetFiltered(GameObject, SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable | (g.tag == "Tile")))
         {
 
         }
    }




Comment
Add comment
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

1 Reply

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

Answer by jdean300 · Feb 14, 2017 at 01:36 AM

GameObject is a type, but not a Type object. This method needs a Type object. You need to use typeof(GameObject) which creates the Type object.

  private void Update()
  {
      foreach(GameObject g in Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable | (g.tag == "Tile")))
      {
  
      }
  }


EDIT: Check comment for error. (g.tag == "Tile") will not work as shown here

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 Bill9009 · Feb 14, 2017 at 02:20 AM 0
Share

Thank you for that, but now, with the script that follows, I am getting the error (The name 'g' does not exist in current context.) with the g.tag part.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 [ExecuteInEdit$$anonymous$$ode]
 
 public class Snap : $$anonymous$$onoBehaviour {
     private void Update()
     {
         foreach(GameObject g in Selection.GetFiltered(typeof(GameObject), Selection$$anonymous$$ode.TopLevel | Selection$$anonymous$$ode.OnlyUser$$anonymous$$odifiable | (g.tag == "Tile")))
         {
             SpriteRenderer spr = g.GetComponent<SpriteRenderer>();
             Transform t = g.transform;
             g.transform.position = new Vector3(
                     (t.position.x - (t.position.x % 1)) + spr.bounds.extents.x,
                     (t.position.y - (t.position.y % 1)) + spr.bounds.extents.y,
                     (t.position.z - (t.position.z % 1)) + spr.bounds.extents.z
                 );
         }
     }
 }
avatar image jdean300 Bill9009 · Feb 14, 2017 at 02:24 AM 0
Share

You can't use g in the line g.tag == "Tile" because it is not defined until you are inside the foreach loop. Add this inside your loop:

 if (g.tag != "Tile")
     continue;

avatar image Bill9009 jdean300 · Feb 14, 2017 at 02:35 AM 0
Share

Alright It worked and I used this.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 [ExecuteInEdit$$anonymous$$ode]
 
 public class Snap : $$anonymous$$onoBehaviour {
     private void Update()
     {
         //This finds all gameObjects I can move
         foreach(GameObject g in Selection.GetFiltered(typeof(GameObject), Selection$$anonymous$$ode.TopLevel | Selection$$anonymous$$ode.OnlyUser$$anonymous$$odifiable  ))
         {
             if (g.tag == "Tile")
             {
                 //These splits the GameObject into a Transform and SpriteRenderer
                 SpriteRenderer spr = g.GetComponent<SpriteRenderer>();
                 Transform t = g.transform;
                 //This changes the objects transform to align to the grid in unity
                 g.transform.position = new Vector3(
                         (t.position.x - (t.position.x % 1)) + spr.bounds.extents.x,
                         (t.position.y - (t.position.y % 1)) + spr.bounds.extents.y,
                         (t.position.z - (t.position.z % 1)) + spr.bounds.extents.z
                     );
             }
         }
     }

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

282 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Distribute terrain in zones 3 Answers

Need a little help with this nullrefexception. 1 Answer

Show only readable textures when selecting a texture with objectified 1 Answer

C# Problem Creating List of Texture2Ds 0 Answers


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