• 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 Dec 30, 2021 at 04:20 AM by Junglerally for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Junglerally · Dec 30, 2021 at 01:41 AM · scripting problembuttoninvalidcastexception

"InvalidCastException: Specified cast is not valid." Error

Hello there! I have a script that will create the menus in my game procedurally so I don't have to make them all myself. It has all been working perfectly fine up until now, and I haven't changed anything that should affect it. I am getting this error :

 InvalidCastException: Specified cast is not valid.
 BuildingMenus.ClearButtons (System.Boolean buildMenu) (at Assets/Scripts/Menus/BuildingMenus.cs:65)
 BuildingMenus.DrawBuildMenu () (at Assets/Scripts/Menus/BuildingMenus.cs:48)
 UnityEngine.Events.InvokableCall.Invoke () (at <4a29708cee654ac0aca16c9d5624806a>:0)
 UnityEngine.Events.UnityEvent.Invoke () (at <4a29708cee654ac0aca16c9d5624806a>:0)
 UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
 UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
 UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
 UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Quite a long one! The thing is, it only happens when I select the tab the SECOND time. So after the game objects are already there. The error is from the ClearButtons() method. My script is this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;
 
 public class BuildingMenus : MonoBehaviour
 {
     private BuildingManager buildingManager;
     private Decorating decorating;
 
     [SerializeField] private GameObject buttonPrefab;
     [SerializeField] private GameObject decorateButtonParent;
     [SerializeField] private GameObject buildButtonParent;
     [SerializeField] private GameObject buttonStart;
     GameObject previousButton;
 
     [SerializeField] private float offset;
 
     private Button[] buttons;
 
     void Start()
     {
         buildingManager = GameObject.Find("Building Manager").GetComponent<BuildingManager>();
         decorating = GameObject.Find("Building Manager").GetComponent<Decorating>();
     }
 
     public void DrawDecorateMenu()
     {
         previousButton = null;
         ClearButtons(false);
         foreach(DecorationAsset decoration in decorating.decorations)
         {
             if (decoration.unlocked)
             {
                 if (previousButton == null) { previousButton = Instantiate(buttonPrefab, buttonStart.transform.position, transform.rotation, decorateButtonParent.transform); }
                 else { previousButton = Instantiate(buttonPrefab, new Vector3(previousButton.transform.position.x + Screen.width/offset, previousButton.transform.position.y, previousButton.transform.position.z), transform.rotation, decorateButtonParent.transform); }
                 previousButton.GetComponent<Image>().sprite = decoration.icon;
                 previousButton.GetComponent<Button>().onClick.AddListener(delegate {decorating.SelectDecoration(decoration); });
                 previousButton.name = decoration.name;
             }
         }
     }
 
     public void DrawBuildMenu()
     {
         previousButton = null;
         ClearButtons(true);
         foreach (CoursePieceAsset piece in decorating.coursePieces)
         {
             if (piece.unlocked)
             {
                 if (previousButton == null) { previousButton = Instantiate(buttonPrefab, buttonStart.transform.position, transform.rotation, buildButtonParent.transform); }
                 else { previousButton = Instantiate(buttonPrefab, new Vector3(previousButton.transform.position.x + Screen.width / offset, previousButton.transform.position.y, previousButton.transform.position.z), transform.rotation, buildButtonParent.transform); }
                 previousButton.GetComponent<Image>().sprite = piece.icon[0];
                 previousButton.GetComponent<Button>().onClick.AddListener(delegate { decorating.SelectCoursePiece(piece); });
                 previousButton.name = piece.name;
             }
         }
     }
 
     public void ClearButtons(bool buildMenu)
     {
         if (buildMenu) {
             foreach (GameObject obj in buildButtonParent.transform) { Destroy(obj); }
         }
         else {
             foreach (GameObject obj in decorateButtonParent.transform) { Destroy(obj); }
         }
     }
 }


I have tabs that you click on to switch between different sections of the building menu. Here is a screenshot of what one of the tabs click events look like if this is relevant at all: alt text

I am assuming that this is something that I have just completely missed and I am just being dumb, but I hope this is enough info to help! If you need to see anything else please let me know.

buttonevents.png (18.7 kB)
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

  • Sort: 
avatar image
0
Best Answer

Answer by Junglerally · Dec 30, 2021 at 04:20 AM

SOLVED BY SOMEONE ON A DISCORD SERVE I AM ON : It is because transforms hold transforms not game objects, so I needed to change the GameObject in the loop to transform. I am sad that I didn't realize this. Thank you to that person, you helped me a lot. Maybe one day, this will help someone else.

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Cannot change the value of this integer that I use for array,Cannot change the value of this one. 0 Answers

Help required for shutting down compiled App 0 Answers

If statement inside FixedUpdate 0 Answers

Add force when button is pressed 2 Answers

OnClick() animation 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