Search Unity

InvalidCastException: Specified cast is not valid.

Discussion in 'Scripting' started by kshitijbagal, May 9, 2021.

  1. kshitijbagal

    kshitijbagal

    Joined:
    Oct 20, 2020
    Posts:
    4
    Hi Everyone
    Am trying to creating an card game in which player can attack on enemy card in Battlezone. if players attack power[A.P] is grater than enemy then the enemy card should be destroy and the difference from should be subtracted from enemy's life points. so i have attached OnMouseOver Event on enemy card that make them attackable only if its players turn and if card can attack and then dragging over from player card to enemy card. but if there are no monster then player can attack directly to enemy's health so i have to create both cases to find who is Target so in my PlayerAttack Script am trying to specify that if you can help me solve that it will be big help

    i suspect its an form of DownCast. but i dont know much about its because am new to unity and C# scripting

    am crating this game based on Cezary_Sharp's Series of crad game creation so if theres any problem in my explaining you can take look at the series in series its working but am trying to optimis the game by my idea

    Error at PlayerAttack.cs(102)
    so thanks for help

    here's the script
    PlayerAttack.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAttack : MonoBehaviour
    6. {
    7.     public int attcakPower;
    8.     public static GameObject Target;
    9.     public static GameObject Enemy;
    10.     public GameObject AttackingBorder;
    11.     public GameObject EnemyCard;
    12.     public bool summoningSickness;
    13.     public bool canAttack;
    14.     public bool IsAttackDone;
    15.     public bool cantAttack;
    16.     public static bool staticTargeting;
    17.     public static bool staticTargetingEnemy;
    18.     public static bool staticTargetingEnemyCard;
    19.     public bool targeting;
    20.     public bool targetingEnemy;
    21.     public bool targetingEnemyCard;
    22.     public bool onlyThisCardAttack;
    23.     public GameObject summoningBorder;
    24.     public bool canBeDestroyed;
    25.     public GameObject EnemyZone;
    26.     public AICardToHand aICardToHand;
    27.     public GameObject PlayersBattleZone;
    28.     public GameObject EnemyBattleZone;
    29.     void Start()
    30.     {
    31.         IsAttackDone=false;
    32.         summoningSickness=true;
    33.         canAttack=false;
    34.         Enemy= GameObject.Find("Enemy HP");
    35.         targeting=false;
    36.         targetingEnemy=false;
    37.         targetingEnemyCard=false;
    38.         EnemyZone=GameObject.Find("Enemy-Battle-Zone");
    39.     }
    40.     // Update is called once per frame
    41.     void Update()
    42.     {    
    43.         if(TurnSystem.isYourTurn==false)
    44.         {
    45.             IsAttackDone=false;
    46.         }
    47.         if (this.GetComponent<ThisCard>().spell==true)
    48.         {
    49.             canAttack=false;
    50.             cantAttack=true;
    51.         }
    52.         if (IsAttackDone==true)
    53.         {
    54.             canAttack=false;
    55.             cantAttack=true;
    56.         }
    57.         if (canAttack==true)
    58.         {
    59.             AttackingBorder.SetActive(true);
    60.         }else
    61.         {
    62.             AttackingBorder.SetActive(false);
    63.         }
    64.         if (TurnSystem.isYourTurn==false && this.GetComponent<ThisCard>().summoned==true)
    65.         {
    66.             summoningSickness=false;
    67.             cantAttack=false;
    68.         }
    69.         if (TurnSystem.isYourTurn==true && summoningSickness==false && cantAttack==false && IsAttackDone==false)
    70.         {
    71.                 canAttack=true;
    72.         }
    73.         else
    74.         {
    75.             canAttack=false;
    76.         }
    77.         targeting=staticTargeting;
    78.         targetingEnemy=staticTargetingEnemy;
    79.         targetingEnemyCard=staticTargetingEnemyCard;
    80.         if (targetingEnemy==true)
    81.         {
    82.             Target=Enemy;
    83.         }
    84.         if (targetingEnemyCard==true)
    85.         {
    86.             Target=EnemyCard;
    87.         }//&&targetingEnemy==true
    88.         if (targeting==true  && onlyThisCardAttack==true)
    89.         {
    90.             Attack();
    91.         }    
    92.     }
    93.     public void Attack()
    94.     {
    95.         if (canAttack==true && IsAttackDone==false && this.GetComponent<ThisCard>().spell==false)
    96.         {
    97.             EnemyBattleZone=GameObject.Find("Enemy-Battle-Zone");
    98.             foreach (GameObject childc in EnemyBattleZone.transform)
    99.             {
    100.                 if (childc.GetComponent<PlayerTarget>().isTarget==true)
    101.                 {
    102.                     EnemyCard=childc;
    103.                 }
    104.             }
    105.             if (Target!= null)
    106.             {
    107.                 if (Target==Enemy)
    108.                 {
    109.                     Debug.Log("This Is Attack");
    110.                     EnemyHp.staticHp-=this.GetComponent<ThisCard>().cardAttack;
    111.                     targeting=false;
    112.                     IsAttackDone=true;
    113.                     canAttack=true;
    114.                 }
    115.             else
    116.             {
    117.                 if (targetingEnemyCard==true)
    118.                 {
    119.                     PlayersBattleZone=GameObject.Find("Battle-Zone");
    120.                     EnemyBattleZone=GameObject.Find("Enemy-Battle-Zone");
    121.                     foreach (Transform child in PlayersBattleZone.transform)
    122.                     {
    123.                         if (cantAttack==false && canAttack==true)
    124.                         {
    125.                             foreach (Transform childs in EnemyBattleZone.transform)
    126.                             {
    127.                                 if (childs.GetComponent<AICardToHand>().IsPlayersTarget=true && TurnSystem.isYourTurn==true &&childs.GetComponent<AICardToHand>().IsAICardSpell==false)
    128.                                 {
    129.                                     if (childs.GetComponent<AICardToHand>().cardAttack<child.GetComponent<ThisCard>().cardAttack)
    130.                                     {
    131.                                         childs.GetComponent<AICardToHand>().hurted=child.GetComponent<ThisCard>().cardAttack;
    132.                                         // hurted=childs.GetComponent<AICardToHand>().cardAttack;
    133.                                         int x;
    134.                                         x=childs.GetComponent<AICardToHand>().cardAttack-=child.GetComponent<ThisCard>().cardAttack;
    135.                                         EnemyHp.staticHp-=x;
    136.                                         IsAttackDone=true;
    137.                                     }
    138.                                     else
    139.                                     {
    140.                                         if (childs.GetComponent<AICardToHand>().cardAttack==child.GetComponent<ThisCard>().cardAttack)
    141.                                         {
    142.                                             child.GetComponent<ThisCard>().Destroy();
    143.                                             childs.GetComponent<AICardToHand>().Destroy();
    144.                                         }
    145.                                         child.GetComponent<ThisCard>().Destroy();
    146.                                     }
    147.                                  
    148.                                 }
    149.  
    150.                             }
    151.                         }
    152.                     }
    153.                     canAttack=true;
    154.                 }
    155.                 }
    156.             }
    157.         }
    158.     }
    159.     public void UntargetingEnemy()
    160.     {
    161.         staticTargetingEnemy=false;
    162.     }
    163.     public void TargetEnemy()
    164.     {
    165.         staticTargetingEnemy=true;
    166.     }
    167.     public void UntargetingEnemyCard()
    168.     {
    169.         staticTargetingEnemyCard=false;
    170.     }
    171.     public void TargetEnemyCard()
    172.     {
    173.         staticTargetingEnemyCard=true;
    174.     }
    175.     public void StartAttack()
    176.     {
    177.         staticTargeting=true;
    178.     }
    179.     public void StopAttack()
    180.     {
    181.        staticTargeting=false;
    182.     }
    183.     public void OneCardAttack()
    184.     {
    185.         onlyThisCardAttack=true;
    186.     }
    187.     public void OneCardAttackStop()
    188.     {
    189.         onlyThisCardAttack=false;
    190.     }
    191. }
    192.  

    PlayerTarget.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerTarget : MonoBehaviour
    6. {
    7.     public GameObject EnemyTargetBorder;
    8.     public bool isTarget;
    9.     public AICardToHand AICardToHand;
    10.     public GameObject Graveyard;
    11.     public bool thisCardCanBeDestroyed;
    12.     void Start()
    13.     {
    14.         Graveyard=GameObject.Find("EnemyGraveyard");
    15.         StartCoroutine(AfterVoidStart());
    16.     }
    17.     public void Target()
    18.     {
    19.         this.GetComponent<AICardToHand>().IsPlayersTarget=true;
    20.         Debug.Log(this.GetComponent<AICardToHand>().IsPlayersTarget);
    21.     }
    22.      public void NotTarget()
    23.     {
    24.         this.GetComponent<AICardToHand>().IsPlayersTarget=false;
    25.         Debug.Log(this.GetComponent<AICardToHand>().IsPlayersTarget);
    26.     }
    27.  
    28.         IEnumerator AfterVoidStart()
    29.     {
    30.         yield return new WaitForSeconds(1);
    31.         thisCardCanBeDestroyed=true;
    32.     }
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         Graveyard=GameObject.Find("EnemyGraveyard");
    37.         GameObject EnemyBattleZone=GameObject.Find("Enemy-Battle-Zone");
    38.         foreach (Transform child in EnemyBattleZone.transform)
    39.         {
    40.             if (child.GetComponent<AICardToHand>().IsPlayersTarget==true)
    41.             {
    42.                 EnemyTargetBorder.SetActive(true);
    43.             }
    44.             if (child.GetComponent<AICardToHand>().IsPlayersTarget==false)
    45.             {
    46.                 EnemyTargetBorder.SetActive(false);
    47.             }
    48.  
    49.         if (this.GetComponent<AICardToHand>().IsAICardSpell==false  && this.GetComponent<AICardToHand>().IsEffectApplied==false)
    50.         {
    51.             thisCardCanBeDestroyed=false;
    52.         }
    53.         }
    54.         if (this.GetComponent<AICardToHand>().hurted>=this.GetComponent<AICardToHand>().cardAttack && thisCardCanBeDestroyed==true &&  this.GetComponent<AICardToHand>().IsAICardSpell==true  && this.GetComponent<AICardToHand>().IsEffectApplied==true)
    55.         {
    56.             this.transform.SetParent(Graveyard.transform);
    57.         }
    58.         if (this.GetComponent<AICardToHand>().IsAICardSpell==true && this.GetComponent<AICardToHand>().IsEffectApplied==true)
    59.         {
    60.             this.transform.SetParent(Graveyard.transform);
    61.         }
    62.     }
    63.  
    64.     public void BeingTarget()
    65.     {
    66.         if (AICardToHand.spell==false)
    67.         {
    68.             isTarget=true;
    69.         }else
    70.         {
    71.             isTarget=false;
    72.         }
    73.     }
    74.     public  void DontBeingTarget()
    75.     {
    76.         if (AICardToHand.spell==false)
    77.         {
    78.             isTarget=false;
    79.         }
    80.     }
    81.     public override bool Equals(object obj)
    82.     {
    83.         return obj is PlayerTarget target &&
    84.                EqualityComparer<GameObject>.Default.Equals(EnemyTargetBorder, target.EnemyTargetBorder) &&
    85.                isTarget == target.isTarget &&
    86.                EqualityComparer<AICardToHand>.Default.Equals(AICardToHand, target.AICardToHand);
    87.     }
    88. }
    89.  
     
    Last edited: May 10, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Generally you can't cast across object hierarchies. You neglect to say what line is causing the issue so I can't say more.

    What you want sounds like an interface: many different types of objects can implement it, and then your call site doesn't care what type of object it is, as long as it has that interface.

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
  3. kshitijbagal

    kshitijbagal

    Joined:
    Oct 20, 2020
    Posts:
    4
    sorry My bad i forgot to mention the line of error its PlayerAttack.cs(102)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    What are the types on each side of that assignment? Would you expect them to be cast-able?

    Some help to fix "Cannot implicitly convert type 'Xxxxx' into 'Yyyy':"

    http://plbm.com/?p=263