Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Having some difficulty finding the answer...

Discussion in 'Scripting' started by Enzo36t, May 27, 2024.

  1. Enzo36t

    Enzo36t

    Joined:
    Aug 30, 2020
    Posts:
    85
    Hello!

    I'm trying to make this line of code include "and" / "or" statement. I don't actually know how to script but I understand what's going on in most parts of the scripts and learning as I make changes to the code in the project. :)

    I've already had a look around online and I can't seem to get something working without copile errors in this script which I hoped would be a basic addition of a line of code to adding a change to the game.

    Here's the line of code.
    if (card.type == V_Card.cardType.Creature) {

    What I want to do is include another card type in with the instruction as they're both similar types, but have a difference in the name. What I hope to do is:

    if (card.type == V_Card.cardType.Creature) or if (card.type == V_Card.cardType.Creature2) {

    Then the script continues to allow the action.

    I'm unsure how to include both in the same section and I hope this is possible as duplicating the section of code didn't work out either.

    When I change the creature type to:
    if (card.type == V_Card.cardType.Creature2) {

    There are no errors and the game will run and the Creature2 cards will work but not the Creature cards.

    I've also tried

    if (card.type == V_Card.cardType.Creature) {
    if (card.type == V_Card.cardType.Creature2) {


    That didn't work and so I think I'm missing something to have them together without a compile issue and it's possibly the way I am including them or I am missing a statement that I can't seem to find online. >.<

    Any help on how I can include Creature2 in with the section of code would be greatly appreciated.

    Thank you so much!
     
    Last edited: May 27, 2024
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    819
    You are missing "boolean logical operators"

    || = OR
    && = AND

    So you'd do

    Code (CSharp):
    1. if (card.type == V_Card.cardType.Creature || card.type == V_Card.cardType.Creature2)
     
    Enzo36t likes this.
  3. Enzo36t

    Enzo36t

    Joined:
    Aug 30, 2020
    Posts:
    85
    Ahh! That works! That's where I went wrong! I am so glad I asked. Thank you so much!
    Could I ask also, how can I put a delay for the code to wait a second or X amount of time?
     
    Last edited: May 27, 2024
  4. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    819
    It depends how you've got your code set up.

    One way is to Invoke a method with a delay:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,371
    ^ ^ ^ Flash is spot-on about this... that's the general solution.

    Noting your original question however, card and/or board and/or turn-based games often need far more complex interconnects in order to cause a series of moves to happen in sequence, all the while properly inhibiting other input from interrupting the motion, etc.

    The reason is if you just use a coroutine or invoke with delay, there's nothing to prevent other stuff happening, where "other stuff happening" could be the game itself moving to the next turn, or the player tapping more buttons.

    The general pattern for this stuff is whenever something is happening you set a flag that is checked by other things that might want to happen (other moves, other input) and inhibits those until the "stuff happening" finishes, at which point it clears the boolean.

    You might want to study some card game tutorials on how to accomplish that, but honestly a lot of card game tutorials simply omit it, making it possible to spam-touch or spam-click the screen and cause the game to malfunction. :)
     
    flashframe likes this.