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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Domedi · Feb 17, 2014 at 01:24 AM · gameobjectcomparison

Comparing game objects for my Tic Tac Toe game.

Hello. I have a tic tac toe game but instead of X's and O's the cubes change color. I am trying to figure out a way to compare a row of three cubes with each other to determine if it is a win then display the 'game over' message, but have been unsuccessful. I tried creating a bool and setting each color to T or F but it displays the game over message before the game even starts. Basically I would like to know the best way to compare three cubes with each other to see if they are the same.

Here is the color changing code, it is attached to each of the 9 cubes on the board:

 public class Cubes : MonoBehaviour
 {    
 
 void OnMouseOver()
 {
 if(Input.GetMouseButtonDown(0)){
     renderer.material.color = Color.red;
     Destroy(GetComponent<MeshCollider>());
     }
 
 else if (Input.GetMouseButtonDown (1)) {
     renderer.material.color = Color.blue;
     Destroy (GetComponent<MeshCollider> ());
             }
     }    
 }

And here is the code for my controller(I'm trying to get it to work with three objects first):

     public class Controller : MonoBehaviour {
     public GUIText gameOverText;
     public GameObject B1;//for boxes 1-9
     public GameObject B2;
     public GameObject B3;
     
     void Start () {
     gameOverText.text = "";        
         }        
     
  void Update() {
     
     if (??????)    
     {
     gameOverText.text = "Game Over";}
         
     }
     }

I hope I explained my problem clearly and it makes sense. If I did not apologies. Thank you.

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

3 Replies

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

Answer by Olgo · Feb 17, 2014 at 05:04 AM

In addition to turning the cube red or blue, i would also associate a number (x) with each color, like 1 or 2. Then you could write some bool operations to check to see if somebody won by comparing the int.

so...

Idea 1:

 //In your Cube file, add a variable that signifies which player has selected the cube
 //for example, make an int called "chosenBy", set chosenBy to 0 for nobody, 1 for player 1, 2 for player 2.
 Cubes cB1 = B1.GetComponent<Cubes>();
 Cubes cB2 = B2.GetComponent<Cubes>();
 Cubes cB3 = B3.GetComponent<Cubes>();
 
 if( cB1.chosenBy == cB2.chosenBy && cB2.chosenBy == cB3.chosenBy ){
   // top three values are equal!
 }

Your Cubes file would look like this:

     public class Cubes : MonoBehaviour
     {
     
         public int chosenBy;
  
         void OnMouseOver()
         {
             if(Input.GetMouseButtonDown(0)){
                 chosenBy = 1;
                 renderer.material.color = Color.red;
                 Destroy(GetComponent<MeshCollider>());
             }
      
             else if (Input.GetMouseButtonDown (1)) {
                 chosenBy = 2;
                 renderer.material.color = Color.blue;
                 Destroy (GetComponent<MeshCollider> ());
             }
         }
     }


------------------------------------------

Idea 2: changing the colors to strings and comparing the strings in the same way... You would not need to change your Cubes class for this solution

 if( B1.renderer.material.color.ToString() == "red" && B1.renderer.material.color.ToString() == "red" && B1.renderer.material.color.ToString() == "red" ){
     myText.text = "Red Wins!";
    }
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
avatar image
0

Answer by Domedi · Feb 17, 2014 at 10:46 PM

So I would populate the ToString with the colors I'm using? This is how I have it now:

    public class Controller2 : MonoBehaviour {
     public GUIText myText;
     public GameObject B1;
     public GameObject B2;
     public GameObject B3;
     
     
     void Start () {
         myText.text = "";
         
     }
     
     // Update is called once per frame
     void Update() {
 
         
         if ( B1.renderer.material.color.ToString("red") == B2.renderer.material.color.ToString("red")) {
             myText.text = "Game Over";
         } 
 
         }
 }

Do I make any changes in my "Cubes" file? For some reason when I do this it will display the "Game Over" text as soon as the game starts, regardless if I click or not. Interesting though, if I use the != instead of == as my comparison operator it will not display the text at all. I tried using the first solution by creating a variable in Cubes and assigning it a value based on which click the user input. Same deal as the ToString solution.

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 Olgo · Feb 18, 2014 at 03:15 PM 0
Share

Side note; you aren't supposed to post further information as an answer, you should make it a comment to my answer. You could also edit your original question. I don't $$anonymous$$d but others will yell at you :)

$$anonymous$$eep in $$anonymous$$d these are just 2 of the simpler solutions that just popped into my head, there are endless ways you could do this.

So depending on which route you wanted to take, you would either...

A: Change your cubes file to have another variable that holds a value to compare.

or

B: Compare the strings using ToString(), adding ".ToString()" to a variable just converts whatever it is into a string of text.

I didn't really include a complete answer because I didn't know your level of understanding. I was just providing an example to point you in the right direction. I will now edit my original response to be more in depth.

Sorry about the 1 million different edits that probably popped up in your email :) You should have all the info you need now.

avatar image Domedi · Feb 18, 2014 at 06:31 PM 0
Share

Got it! It didn't occur to me that all the cubes were automatically set to 0 so the game over sign flashed before the game even got going. I changed their default values in the inspector to anything but 1 or 2. That coupled with your great help got it working perfectly. Thanks a heap for your time and patience and apologies for misposting my earlier reply. Hope I got it right this time. Cheers.

avatar image Olgo · Feb 18, 2014 at 06:42 PM 0
Share

no problem. glad to help

avatar image
0

Answer by himanshugupta159 · Aug 16, 2018 at 01:15 PM

link text Check the link i have written a blog related to How to make a Tick Tack Toe game. If you need any help comment on the blog i will be there.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# Rotate GameObjects Regardless of List Size 2 Answers

server side highscores tutorial: text on gameobject question 1 Answer

How to Destroy a gameobject on collision 3 Answers

How to make a gameobject shake. 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