Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 check35 · Mar 17, 2021 at 06:42 PM · selectioncombineplanetpaintmanagement

Need to combine several gameobject sets into a specific result. Please help!!

I am trying to build a god-terraforming game where you create biomes. And when you combine biomes it should can result with different kind of biomes. For example in the first screenshot Grassland and Mud created a Wetland.

All my triangles are independent gameobjects. And I'm selecting them with a basic sphere collider and listing them as selected. But in my build I can make only 1 combination at a time. But I need to be able to combine couple of them at the same time. How can I achieve that? I am struggling with this for days.. Any kind of help is appreciated. And sorry about the messyness of the code Im kind of a newbie.

Also my combinations are scriptable objects with 2 ingridients and 1 result.

this is my card class.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class card : MonoBehaviour
 {
     public GameObject brushPrefab;
 
     bool isMagnifying = false;
     bool onDrag = false;
     bool brushActive = false;
     bool canCombine;
 
     brush brushData;
     GameObject canvas;
     GameObject manager;
     GameObject cardHolder;
     GameObject brush;
     GameObject cardText;
     Text nameText;
 
     Vector3 startScale;
 
     public string cardEffect;
     public Material effectMat;
     public Color effectColor;
     public float effectRadius = 1;
     public List<GameObject> objectsToSpawn;
 
     string combinationName;
     Color combinationColor;
     public CombinationList combinationList;
     List<StateCombination> combinations;
     public List<string> existingStates;
 
     // Start is called before the first frame update
     void Start()
     {
         canvas = GameObject.Find("Canvas");
         manager = GameObject.Find("Main Camera");
         cardHolder = GameObject.Find("CardHolder");
         cardText = transform.GetChild(0).gameObject;
         nameText = cardText.GetComponent<Text>();
         startScale = GetComponent<RectTransform>().localScale;
         combinations = combinationList.combinations;
 
         effectMat.color = effectColor;
         nameText.text = cardEffect;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!existingStates.Contains(cardEffect))
         {
             existingStates.Add(cardEffect);
         }
         if (onDrag)
         {
             if(brushActive == false)
             {
                 brush = Instantiate(brushPrefab);
                 brush.transform.localScale *= effectRadius;
                 brush.SetActive(true);
                 brushData = brush.GetComponent<brush>();
                 brushActive = true;
             }
 
             manager.GetComponent<gameManager>().FindBrush();
 
             transform.position = Input.mousePosition;
             GetComponent<RectTransform>().localScale = startScale;
             if (manager.GetComponent<gameManager>().mouseOnPlanet == true)
             {
 
                 CheckForCombinations();
                 GetComponent<Image>().enabled = false;
                 
                 //planet.GetComponent<Planet>().GetTriangles(effectName, combinationName, objectsToSpawn, effectSize, extrudeSize, propCount, effectColor, combinationColor);
             }
             else
             {
                 GetComponent<Image>().enabled = true;
                 
             }
         }
         
     }
 
     public void Magnify()
     {
         //Debug.Log("bakcam");
         GetComponent<RectTransform>().localScale = GetComponent<RectTransform>().localScale + GetComponent<RectTransform>().localScale;
         isMagnifying = true;
     }
 
     public void unMagnify()
     {
         //Debug.Log("bakmıcam");
         GetComponent<RectTransform>().localScale = startScale;
         isMagnifying = false;
     }
 
     public void Drag()
     {
         if (isMagnifying == true)
         {
 
             transform.SetParent(canvas.transform);
             onDrag = true;
         }
 
     }
 
     public void unDrag()
     {
         if (manager.GetComponent<gameManager>().mouseOnPlanet == false)
         {
             onDrag = false;
             brushActive = false;
             Destroy(brush);
 
             transform.SetParent(cardHolder.transform);
             transform.position = new Vector3(transform.position.x, transform.position.y, cardHolder.transform.position.z);
             GetComponent<Image>().enabled = true;
         }
         else
         {
             ActivateCardEffect();
             onDrag = false;
             brushActive = false;
             canCombine = false;
 
             Destroy(brush);
             GetComponent<Image>().enabled = false;
             Destroy(this.gameObject, 0.01f);
         }
 
     }
 
     public void CheckForCombinations()
     { 
         if(brushData.trisToCombine.Count > 0) // check for combinable list
         {
             for (int i = 0; i < brushData.trisToCombine.Count; i++) // get all states in combinable list
             {
                 if (!existingStates.Contains(brushData.trisToCombine[i].GetComponent<Tri>().plotState))
                 {
                     existingStates.Add(brushData.trisToCombine[i].GetComponent<Tri>().plotState);
                 }
             }
 
             for (int i = 0; i < combinations.Count; i++) //search for all combinations
             {
                 if (CompareLists(existingStates, combinations[i].StatesToCombine)) // if a combination matches
                 {
                     Debug.Log("should create" + combinations[i].resultState);
                     canCombine = true;
                     combinationColor = combinations[i].resultColor;
                     combinationName = combinations[i].resultState;
                     break; //stop searching
                 }
                 else
                 {
                     // no combination
                     Debug.Log("so close");
                     canCombine = false;
                 }
             }
 
         }
         else
         {
             // no combination
             Debug.Log("nopee");
             existingStates.Clear(); // clear list
             canCombine = false;
         }
 
        
     }
 
     public void ActivateCardEffect()
     {
         //basic application of card effect to selected triangles
         for(int i = 0; i < brush.GetComponent<brush>().selectedTris.Count; i++)
         {
             brush.GetComponent<brush>().selectedTris[i].GetComponent<Tri>().ChangePlotCharacter(cardEffect, effectColor);
             
         }
 
         if (canCombine) 
         {
             for (int i = 0; i < brushData.trisToCombine.Count; i++)
             {
                 brush.GetComponent<brush>().trisToCombine[i].GetComponent<Tri>().ChangePlotCharacter(combinationName, combinationColor);
 
             }
         }
         else
         {
             for (int i = 0; i < brushData.trisToCombine.Count; i++)
             {
                 brush.GetComponent<brush>().trisToCombine[i].GetComponent<Tri>().ChangePlotCharacter(cardEffect, effectColor);
 
             }
         }
     }
 
     public void SpawnObject(List<GameObject> possiblePlots)
     {
         Instantiate(objectsToSpawn[Random.Range(0, objectsToSpawn.Count)], possiblePlots[Random.Range(0, possiblePlots.Count)].transform);
     }
 
     public static bool CompareLists<T>(List<T> aListA, List<T> aListB)
     {
         if (aListA == null || aListB == null) //aListA.Count != aListB.Count
             return false;
         if (aListA.Count == 0)
             return true;
         Dictionary<T, int> lookUp = new Dictionary<T, int>();
         // create index for the first list
         for (int i = 0; i < aListA.Count; i++)
         {
             int count = 0;
             if (!lookUp.TryGetValue(aListA[i], out count))
             {
                 lookUp.Add(aListA[i], 1);
                 continue;
             }
             lookUp[aListA[i]] = count + 1;
         }
         for (int i = 0; i < aListB.Count; i++)
         {
             int count = 0;
             if (!lookUp.TryGetValue(aListB[i], out count))
             {
                 // early exit as the current value in B doesn't exist in the lookUp (and not in ListA)
                 return false;
             }
             count--;
             if (count <= 0)
                 lookUp.Remove(aListB[i]);
             else
                 lookUp[aListB[i]] = count;
         }
         // if there are remaining elements in the lookUp, that means ListA contains elements that do not exist in ListB
         return lookUp.Count == 0;
     }
 }


this is the brush class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class brush : MonoBehaviour
 {
     public List<GameObject> selectedTris;
     public List<GameObject> trisToCombine;
 
     private void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.GetComponent<Tri>() != null) //check if there is any triangle
         {
             other.gameObject.GetComponent<Tri>().selected = true; //highlight all tris inside the circle
 
             if (other.gameObject.GetComponent<Tri>().plotState != "Arid") //if tri is not arid
             {
                 trisToCombine.Add(other.gameObject); // add to combinable list
             }
             else // if it is
             {
                 selectedTris.Add(other.gameObject); // add to normal list
             }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.GetComponent<Tri>() != null) // check for triangles again
         {
 
             other.gameObject.GetComponent<Tri>().selected = false; // unlight outside the circle
 
             if (other.gameObject.GetComponent<Tri>().plotState != "Arid") 
             {
                 trisToCombine.Remove(other.gameObject);
             }
             else
             {
                 selectedTris.Remove(other.gameObject);
             }
         }
     }
 
 }
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

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

113 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change GUILayout.Button font color 3 Answers

Fatal Error in gc : GetThreadContext Failed 0 Answers

Making a mesh transparent 1 Answer

How can I paint a picture and then compare it to another? 1 Answer

how to combine 2 quaternion components? 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