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 /
  • Help Room /
avatar image
0
Question by ImKyleConway · Jul 25, 2020 at 08:06 PM · variablestagsinstancedrag-and-dropfindgameobjectswithtag

Public GameObject set to any instance with a tag

Hi, I am working on a small puzzle game that requires a player to drag and drop sprites from a bank of sprites, to build a path.

. alt text

.

To do so I am following a simple drag and drop puzzle game tutorial. However with this guide they are using 5 or so unique silhouettes, with the dragged sprites having a public GameObject set to the specific silhouette it is meant to be placed on, and snaps to it if close enough. I am not completely new to coding, and have been working on my project for a few months now, while this tutorial makes complete sense, the added complexity of my mini game (i.e repeating patterns, and sprites) does not quite work. I was hoping to use the same method, or at least one close enough as to not have to make a complex system (for me) for such a minor puzzle/mini game. So my question is, is it possible to set something like a public variable to be any object that contains a specific tag? For instance my goal would be to have something like:

.

public GameObject correctSpace;

.

...on a script for the banked sprites, and when within a certain distance of a black square (which are GameObjects) that contains the tag "HorizontalLine" for example, to say 'OK, this specific instance of this GameObject is the correctSpace GameObject. And likewise forget that info if it was to be moved away. Ideally, it would also be public so I could change it in the inspector for each "puzzle piece" and not have to hard code which tag each unique piece is looking for.

.

Thanks!

example.png (20.0 kB)
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 ImKyleConway · Jul 26, 2020 at 12:18 AM

Solved, maybe not the best way but I gave both object box collider 2d/ Rigid Body 2d Components, and the same tag, in this instance, "HLine", then I added this code:

 public GameObject correctForm;
 public string correctTileType;
     
      void OnCollisionEnter2D(Collision2D collision)
             {
            if ((collision.gameObject.tag == correctTileType) && (gameObject.tag == correctTileType))
                 {
                     correctForm = collision.gameObject;
                     
                 }
             }
 

Basically I used a public string to be able to tell the script which tag to look for through the inspector (YES!) instead of having 6 different if statements, or god forbid 6 different script all together. There is a Var called "correctForm" which ever object that is set to will allow the banked items to snap to its position. This code checked what the tag of the object I am colliding which is, in this "HLine", and what my objects tag is, also "HLine". And if they are the same, that object I am colliding with becomes the new public GameObject correctForm;. I still have to manually set the tags for the banked objects and the black squares that will accept that shape, but I am happy to avoid hard coding it.

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

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

210 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

Related Questions

Script that manages all the variables depending on the difficulty doesnt update on all the scripts using those variables. 1 Answer

Using variables from another script. 0 Answers

Issues with dropping an object on multiple objects separetly - Simple 2d Drag & Drop 0 Answers

Screen clear using tags 0 Answers

access to public methods of instance 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