Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 question was closed Oct 06, 2015 at 08:21 PM by Jiji_ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jiji_ · Oct 03, 2015 at 08:13 PM · c#selectionselecting objects

Selecting and comparing two gameObj at runtime?

Hey, So I am a beginner here. I am making a memory card game. I have a board with cards on it. I have wrote the script but I am kinda stuck, because I dont know how to select two GameObj during the runtime and compare them Comparing I think easy if I know how to select two GameObj Here is my code: THIS SCRIPT WILL BE ATTACHED TO THE BOARD I THINK // I CAN EVEN ATTACH IT TO THE CARD RIGHT?

 public class FlipAD : MonoBehaviour
 {
 
     public GameObject gameOb ; //SELECT DURING RUNTIME?
     public GameObject gameOb1;//SAME?
 
 
     void Start(){
         //I think here where should I put my selecting two gameObjects function but I dont know how to write it?
 
     }
     void Update(){
 
 
     }
     void OnMouseDown(){
 
         StartCoroutine (LoopRotation (180f)); //TO ROTATE GameOb 1
         StartCoroutine (LoopRotation2 (180f)); //TO ROTATE GameOb 2 I know it's wrong (If someone can also give me a tip)
     }
 
     IEnumerator LoopRotation(float angle)
     {
         float rot = 0f;
         float dir = 1f;
 
             while(rot < angle)
         {
             gameOb.Rotate(new Vector3(0,90,0) * Time.deltaTime);
                 yield return null;
                 rot= rot+( 90f * Time.deltaTime);
             CheckT ();
         }
     }
     IEnumerator LoopRotation2(float angle)
     {
         float rot = 0f;
         float dir = 1f;
         
         while(rot < angle)
         {
             gameOb1.Rotate(new Vector3(0,90,0) * Time.deltaTime);
             yield return null;
             rot= rot+( 90f * Time.deltaTime);
             CheckT ();
         }
     }
     void CheckT(){ //TO COMPARE THE TWO OBJECTS
 
         if (gameOb.tag == gameOb1.tag) {
             Destroy(gameOb);
             Destroy(gameOb1);
         
         }
 
     }
 }
Comment
Add comment · Show 1
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 Trlgger · Oct 05, 2015 at 10:30 PM 0
Share

Did any of these methods work for you?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Trlgger · Oct 03, 2015 at 09:26 PM

I wouldn't compare the gameObjects, I would create an array of values and compare the elements (by index number) and create or destroy gameObjects based off the results.

Example1:

//set this at the top to allow lists

using System.Collections.Generic;

public class Checker : MonoBehaviour {

 [System.Serializable]
 public class ObjectData{
     public GameObject gameObj;
     public int gameObjValue = 0;
 }

 //Set these in the inspector

 public List<ObjectData> objectList;
 private bool checkOnce = false;

 //Make sure not to compare an object with the same object
     //Set these to the objects index you want to compare

 public int compare1 = 0;
 public int compare2 = 1;

 void Start(){
     //If you want to add objects dynamically
     //objectDataList.Add(yourObject1);
     //objectDataList.Add(yourObject2);

     //Set the numbers you want to compare
     //compare1 = 2;
     //compare2 = 0;
 }

 void Update(){
     //Check if rotation is less then specified angle
     if(rot < angle){
         //Turn toggle on
         checkOnce = true;
     }
     if(checkOnce){
         //Check Objects
         Check();

         //Turn the toggle off
         checkOnce = false;
     }
 }

 //Check rotation
 void Check(){
     if(objectList[compare1].gameObjValue == objectList[compare2].gameObjValue){

         //Destroy objects
         Destroy(objectList[compare1].gameObj);
         Destroy(objectList[compare2].gameObj);


         //Remove from list
         objectList.RemoveAt(compare1);
         objectList.RemoveAt(compare2);

     }
 }

}

You could create a class with a dynamic list like I did if you want to add and remove on the fly, or a simpler alternitive.

Example2:

public class Checker : MonoBehaviour {

 public GameObject obj1;
 public GameObject obj2;

 public int objValue1 = 1;
 public int objValue2 = 1;

 void Start(){
 }

 void Update(){
     //Check if rotation is less then specified angle
     if(rot < angle){
         //Turn toggle on
         checkOnce = true;
     }
     if(checkOnce){
         //Check Objects
         Check();

         //Turn the toggle off
         checkOnce = false;
     }
 }

 //Check rotation
 void Check(){
     if(objValue1 == objValue2){
         Destroy(obj1);
         Destroy(obj2);
     }
 }

}

Never tested the scripts but you get the point.

I would also look into object pooling instead of destroying objects if you plan on doing it all the time.

Comment
Add comment · Show 5 · 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 Trlgger · Oct 03, 2015 at 09:45 PM 0
Share

I should add that you need to declare the List before adding elements of you will get a null reference exception

avatar image Jiji_ · Oct 04, 2015 at 10:02 AM 0
Share

I can see what you did there and pretty nice idea. But I still don't understand how is that going to make me select the game Objects during the run time x.x

avatar image Trlgger · Oct 04, 2015 at 03:41 PM 0
Share

I'm not sure what you mean by select . Do you want to click on the game object and it becomes object1 and click on another that becomes object2?

Public boolean selected=false; Void On$$anonymous$$ouseDown(){selected=true;}

You need to assign a box collider to your object and set it to trigger. Then create a new script, put this function in the script and assign it to each object you wish to select.

private yourscriptname objectScript;

Add this to the top of your first script. Using the dynamic list option and adding all the objects to the list on start do the following in update(for now).

for(Int I =0;I < objectList.Count;I++){ objectScript = objectList[i].gameObj.GetComponent< yourscriptname > (); If(objectScript.selected){ selected1 = objectlist[I].gameObj; objectScript.selected = false; } }

Using this you can figure out how to select the way that best fits your needs.

avatar image Jiji_ · Oct 06, 2015 at 08:20 PM 0
Share

Thanks a lot, I couldn't do exactly what I want. But I used the idea you gave me which is giving each object values and compare using the values ! Thank you!

avatar image Afassolas · Feb 28, 2016 at 07:34 AM 0
Share

@Trigger this looks like something I could use here link text can you help me with this ?

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What's the difference between Selectable.Select() and EventSystem.current.SetSelectedGameObject()? 0 Answers

I need help understanding why this variable returns null. 1 Answer

RTS rectangle selection system 3 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