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 /
  • Help Room /
avatar image
0
Question by PolymathicIndustries · May 15, 2021 at 08:00 PM · spawnmouseclickhighlighthighlighting

Spawning game piece correctly to allow highlighting

I am trying to make a board game and I have made some progress so far, being able to spawn game pieces onto various transforms (serving as my board grid cells). Next, I am trying to highlight each piece once I click/select them and then deselect in a similar manner. I am creating a highlight effect by placing 2 game pieces (spheres) in the same physical space, but my "highlighted" version is set to inactive. I have even created a toggle between the two, but my problem is that when I am spawning new pieces the new ones do not allow themselves to be recognized as game pieces and so only the original piece on the board is able to be clicked still.

Here is my spawn:

 public GameObject prefab;
     public GameObject Highlight;
     //public Transform[] spawnPoints;
     public List<Transform> spawnPoints;
 
 
     // Click the "Instantiate!" button and a new `prefab` will be instantiated
     // somewhere within -10.0 and 10.0 (inclusive) on the x-z plane
     /*void OnGUI()
     {
         if (GUI.Button(new Rect(10, 10, 100, 50), "Instantiate!"))
         {
             var position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
             Instantiate(prefab, position, Quaternion.identity);
         }
     }*/
 
     void OnGUI()
     {
         //int spawn = Random.Range(0, spawnPoints.Length);
         int spawn = Random.Range(0, spawnPoints.Count);
 
         if (GUI.Button(new Rect(10, 10, 100, 50), "Instantiate!"))
         {
            
 
             //var position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
             var SpawnedPiece = Instantiate(prefab, spawnPoints[spawn].transform.position, Quaternion.identity);
             var Highlighted = Instantiate(Highlight, spawnPoints[spawn].transform.position, Quaternion.identity);
             Highlighted.transform.SetParent(SpawnedPiece.transform);
             Highlighted.SetActive(false);
             SpawnedPiece.tag = "Game Piece";
             Highlighted.tag = "Game Piece";
             spawnPoints.RemoveAt(spawn);
         }
     }

Here is my highlight:

 public GameObject GamePiece;
     public GameObject Highlighted;
     public int minRandom;
     public int maxRandom;
 
     // Start is called before the first frame update
     void Start()
     {
         GamePiece.gameObject.SetActive(true);
         Highlighted.gameObject.SetActive(false);
         //AddPhysics2DRaycaster();
 
     }
 
 
 void Update()
     {
   
         if (Input.GetMouseButtonDown(0))
         {
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 100))
             {
                 // whatever tag you are looking for on your game object
                 if (hit.collider.tag == "Game Piece")
                 {
                     Debug.Log("---> Hit: ");
                     StartCoroutine(Waiter());
                 }
                 else
                 {
                     StartCoroutine(GoBack());
                 }
             }
         }
 IEnumerator Waiter()
     {
         int wait_time = Random.Range(minRandom, maxRandom);
         yield return new WaitForSeconds(wait_time);
         //print("I waited for " + wait_time + "sec");
         GamePiece.gameObject.SetActive(false);
         Highlighted.gameObject.SetActive(true);
     }
 
  IEnumerator GoBack()
     {
         int wait_time = Random.Range(minRandom, maxRandom);
         yield return new WaitForSeconds(wait_time);
         //print("I waited for " + wait_time + "sec");
         GamePiece.gameObject.SetActive(true);
         Highlighted.gameObject.SetActive(false);
     }
 }

There might be bracket issues above, but that is just because this is pasted from mobile, there are no errors on computer. Can anyone tell me why my other game pieces are not highlighting?

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

161 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 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

Instantiate PreFab on trigger location 0 Answers

UI Button text highlighted 1 Answer

How to identify players on Photon Unity? 2 Answers

Bullet spawning at high Player speed 1 Answer

NetworkServer is not active. Cannot spawn objects without an active server. 0 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