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 AcornFox · Sep 04, 2014 at 09:33 AM · gameobjectarrayfindgameobjectswithtag

How to call Class specific methods from an object stored in a GameObject[]?

Hey all.

I'm brand new to Unity, so I'm just doing a Tic Tac Toe game to learn the basics.

Everything is working up until I try to reference a Square in the array. I'm having trouble finding a way to store object (Square) references in such a way they can be accessed from their container, called Square_Boss, which is just an empty GameObject with an attached script. The Squares are Cubes I have all set up in the editor; a 2D scene.

I plan to use Square_Boss to hold the win condition logic, which is why I need the array in the first place.

 public class Square_Boss : MonoBehaviour
 {
     public GameObject[] squares;
 
 
         //initialization
         void Start ()
         {
                 squares = GameObject.FindGameObjectsWithTag ("SQU");
         }
 
         public static void Win ()
         {
                 int[] states = new int[9];
 
                 for (int i = 0; i < squares.Length; i++) {
                         states [i] = squares [i].getClickedBy ();
                         Debug.Log (states [i]);
                 }
         }
 }
 

The Square class looks like this:

 public class Square : MonoBehaviour
 {
         private bool isClicked = false;
         private int clickedBy = 0;
         private Color[] theColors = {Color.white, Color.red, Color.blue};
         private static int playerNumber = 1;
         private static int clicks = 0;
 
         // Use this for initialization
         void Start ()
         {
                 renderer.material.color = theColors [0];
         }
 
         private void OnMouseDown ()
         {
                 if (clicks < 9) {
                         if (!isClicked) {
                                 SetState ();
                         }
                 }
         else SendMessageUpwards("Win");
         }
 
         private void SetState ()
         {
                 if (playerNumber == 1) {
                         renderer.material.color = theColors [1];
                 } else {
                         renderer.material.color = theColors [2];
                 }
                 isClicked = true;
                 clickedBy = playerNumber;
                 ChangePlayer ();
         }
 
         private void ChangePlayer ()
         {
                 if (playerNumber == 1) {
                         playerNumber = 2;
                 } else {
                         playerNumber = 1;
                 }
         }
 
         public bool getClicked ()
         {
                 return isClicked;
         }
 
     public int getClickedBy ()
     {
         return clickedBy;
     }
 }
 

How do I make it so I can store references to the Squares and call their public methods from the Square_Boss script? When I try to make the array type Square[] it gives me an error about implicit casting or something (can't remember exactly, I tried that a while ago).

This is my first question on here, so I apologize for the noob mistakes. I looked around for answers but none of them made sense to me out of context. Also, sorry if the indents are weird... I made the mistake of telling MonoDevelop to format my code for me. =P

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

Answer by Graham-Dunnett · Sep 04, 2014 at 09:36 AM

See: http://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html

Basically, classes you write derive from Component. Think of your script/class as being a component.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

HELP Find gameObject With tag in another array 1 Answer

Array Question Length (C#) 8 Answers

store GameObject.FindGameObjectsWithTag in an array 2 Answers

Checking if a position is occupied in 2D? 1 Answer

Get all other GameObjects (not the one script is attached to) 2 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