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 djwhiting · Sep 09, 2014 at 05:55 PM · c#magic

Unity for coders

I’ve been using Unity for a few years now, and while I can start off a project well I always seem to run into the same issues that make me feel like I’m not using it correctly.

For example, I’ve gone back to basics and I’m trying to create a simple Minesweeper game. If I were creating this anywhere else I’d define each square as an object with different properties and methods like mined, number of neighbour mines, reveal, explode, etc. In Unity, I’ve got prefab models for all the graphics. I’ve got a script that’s attached to the camera that sets up the grid and it all looks fine. I’ve got a script attached to the tile prefab that has methods like Reveal. But now that I’m trying to get things to interact with each other, I’m really struggling.

For example, I’d love to have the tiles in an array and then use recursion for reveals: (pseudo code so don’t focus on it)

 public void Reveal() {
   renderer.enabled = false;
   if (tiles[x-1,y].value == 0 ) { // repeat for all other neighbours
     tiles[x-1,y].Reveal();
   }
 }
 

But I can’t just do that because the tile is a GameObject and I can’t talk directly to the code. I can probably do it through the Inspector and dragging and dropping the script into a public variable, or using GetComponent and types, but it doesn’t seem like the right way to go. What would be really simple in plain JavaScript, for example, takes a lot more effort in Unity. What’s the secret sauce that I’m missing? Can someone help me have that ding moment where it all slots together?

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

2 Replies

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

Answer by Kiwasi · Sep 09, 2014 at 07:50 PM

There is no simpler way. You access another component with a reference. You get a reference via the inspector, or by using GetComponent. This is not really any different to regular object orientated programming. There has never been a way to access an instance of a class without having a reference to the class from somewhere.

It is quite common to store references to components, instead of calling GetComponent every frame. In your case it might be relevant for each GameObject to have a reference to each of its neighbours. You would set this up in Awake or Start. Another alternate structure that pops to mind is to have a single manager class that stores all of the references by x, y position, you can then access each one by index.

Another common alternative is to write all of your code as regular C# and just use your MonoBehaviours as the final interface. It is possible to build the entire game with only a thin layer between the game code and Unity. You still can't magically talk to code you have no reference to.

Comment
Add comment · Show 1 · 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 djwhiting · Sep 09, 2014 at 10:43 PM 0
Share

Perfect. I was just feeling Unity was getting in my way when ever I tried anything, but that's given me some ideas. Cheers.

avatar image
0

Answer by tanoshimi · Sep 09, 2014 at 05:57 PM

Unity Answers is intended for specific, technical questions that can be answered. This sounds like a discussion topic that would be better suited to The Forums.

However, I'm not quite sure what the problem is - what do you mean by "the tile is a GameObject and I can’t talk directly to the code"?

Comment
Add comment · Show 1 · 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 djwhiting · Sep 09, 2014 at 06:09 PM 0
Share

I create a cube, squash it, call it "tile", make it a prefab, stick on a script that can make it disappear when it's clicked, great. Now I want dozens of those tiles on the screen and I want to be easily able to iterate over them, so I instantiate those prefabs into an array of GameObjects. But now I want one tile to be able to run methods of neighbouring tiles, and to do this the only way I can find is something like GetComponent to access the attached script. It all feels back to front and I'm sure there should be a simpler way.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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