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 Maerdyn · May 25, 2014 at 02:16 PM · tilegenericsreference-other-object

Accessing tiles in another class

Greetings,

My apologies if this question has been asked before. I've searched far and wide, and even though I do find answers, I either don't understand or they don't give me the answer that I need.

I'm keeping a grid of tiles that I am trying to refer back to, after creation. My first step, is this :

 using UnityEngine;
 using System.Collections;
 
 public class copyPlane : MonoBehaviour {
     public Transform  myPrefab;
     public int width;
     public int height;
     public float tileWidth;
     public float tileHeight;
     public float tileGap;
     public Vector3 startLocation;
     public TileList tiles;
     // Use this for initialization
     void Start () {
         for (int iX = 0; iX< width; iX++) 
         {
             for(int iY = 0; iY < height; iY++)
             {
                 Vector3 loc = new Vector3();
                 loc.x = startLocation.x + ((tileWidth + tileGap) * iX);
                 loc.z = startLocation.z + ((tileHeight + tileGap) * iY);
                 loc.y = startLocation.y;
                 Object nPrefab = Instantiate (myPrefab, loc, myPrefab.rotation);
                 nPrefab.name = "tile_" + iX.ToString () + ";" + iY.ToString ();
                 tiles.AddTile (iX, iY, loc);
             }
         }
 
         //Object nPrefab = Instantiate (myPrefab, new Vector3(6,1,20), myPrefab.rotation);
 
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 }

That works fine. Note the " public TileList tiles;". Then, in order to keep track of my tiles, I created this class :

  using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public  class TileList {
     
     
     
         public List<TileElement> tiles;
         // Use this for initialization
         void Start () 
         {
             tiles = new List<TileElement> ();
         }
     
         public void AddTile(int x, int y, Vector3 location)
         {
             TileElement newTile = new TileElement (location, x, y);
             tiles.Add (newTile);
         }
         // Update is called once per frame
         void Update () {
         
         }
     }
     
     public class TileElement
     {
         public Vector3 location;
         public int x;
         public int y;
         
         public TileElement(Vector3 _location, int _x, int _y)
         {
             location = _location;
             x = _x;
             y = _y;
         }
     }

Super. Now that all populates on launch, and it makes me happy. However, now that I do have that list populates, and I want to move another object to tile X;Y ... How do I refer back to that list? I can't seem to access that class, as I would normally do in C#.

I think my biggest problem here is that I'm still thinking in normal c# terms, and don't have my mind wrapped around the Unity environment/concept.

Any help, pointers, death threats or girlfriend telephone numbers are welcome.

Regards, Maerdyn

EDIT : Properly encapsulated [code][/code]

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 Fornoreason1000 · May 25, 2014 at 02:23 PM 0
Share

This is technically a duplicate, but where are you accessing that list? Do you mean the TileList class of the generic list property inside it?

avatar image Maerdyn · May 25, 2014 at 02:55 PM 0
Share

No, not from the same class. $$anonymous$$aybe my complete idea of the system is wrong here. What I want to do, is have a grid of tiles. Different tiles can have different objects on them. When another object comes on to the tile, I want that tile to trigger. I also want to be able to tell an object to move to a certain tile. In other words, I'll need to reference to a tile in order to get his vector. For that, I'll either need a global List's to refer to, or I must be able to pass that list between objects. For all that I know, I might need to redesign this from scratch. If so, I'm open for that option, if someone can steer me in the right direction. This is my first shot at designing my own game engine from scratch, so I do realize there's a huge possibility here that I'm doing it completely wrong.

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Extending a Singleton base class (UJS) (?) 2 Answers

How to convert the retargeted animation back to Generic animation? 0 Answers

Why do I have to use a generic parameter when creating generic functions? 3 Answers

Passing a generic Array with unknow dimension 4 Answers

Are Unity distances that small? 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