Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Interactive Switches on and off

Discussion in 'Scripting' started by hledward7, Feb 25, 2021.

  1. hledward7

    hledward7

    Joined:
    Jul 9, 2019
    Posts:
    8
    i create a interactive switch for the game but the player cannot trigger the toggle i follow the YouTube tutorial
    Code (CSharp):
    1. public class Switch1 : MonoBehaviour
    2. {
    3.  
    4.     public GameObject Target;
    5.     public string OnMessage;
    6.     public string OffMessage;
    7.     public bool IsOn ;
    8.  
    9.     Animator myanimator;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         myanimator = GetComponent<Animator>();
    15.     }
    16.     public void TurnOn()
    17.     {
    18.         if (!IsOn)
    19.         { SetState(true); }
    20.     }
    21.     public void TurnOff()
    22.     {
    23.         if (IsOn)
    24.         { SetState(false); }
    25.  
    26.     }
    27.     public void Toggle()
    28.     {
    29.         if (IsOn)
    30.         { TurnOff();}
    31.         else
    32.         { TurnOn(); }
    33.     }
    34.  
    35.  
    36.     void SetState(bool on)
    37.        {
    38.      
    39.         IsOn = on;
    40.         myanimator.SetBool("On", on);
    41.         if (on)
    42.         {
    43.        
    44.             if (Target != null && !string.IsNullOrEmpty(OnMessage))
    45.             {
    46.                 Target.SendMessage(OnMessage);
    47.              }
    48.         }
    49.         else
    50.         {
    51.         if (Target != null && !string.IsNullOrEmpty(OffMessage))
    52.             {
    53.                     Target.SendMessage(OffMessage);
    54.             }
    55.         }
    56.     }
    57.  
    58.  
    59. }
    Code (CSharp):
    1. public class Door1 : MonoBehaviour
    2. {
    3.  
    4.     public bool IsOpen;
    5.     Animator myAnimator;
    6.     Collider2D myCollider;
    7.  
    8.     void Start()
    9.     {
    10.         myAnimator = GetComponent<Animator>();
    11.         myCollider = GetComponent<Collider2D>();
    12.     }
    13.  
    14.     public void Open()
    15.     {
    16.         if (!IsOpen)
    17.             SetState(true);
    18.     }
    19.     public void Close()
    20.     {
    21.      
    22.         if (IsOpen)
    23.             SetState(false);
    24.     }
    25.     public void Toggle()
    26.     {
    27.         if (IsOpen)
    28.         { Close(); }
    29.         else
    30.         {  Open();}
    31.     }
    32.     void SetState(bool open)
    33.     {
    34.         IsOpen = open;
    35.         myAnimator.SetBool("Open",open);
    36.         myCollider.isTrigger = open;
    37.     }
    38. }
    39.  
    Code (CSharp):
    1. public class Player1 : MonoBehaviour
    2. {
    3.     public float speed;
    4.     public Collider2D coll;
    5.  
    6.     [SerializeField]
    7.     private Rigidbody2D rb;
    8.     private Animator anim;
    9.  
    10.     public Transform groundCheck;
    11.  
    12.     public LayerMask whatisGround;
    13.  
    14.     private bool grounded;
    15.     private int extraJump;
    16.     public float JumpForce;
    17.     public Transform muzzle;
    18.  
    19.     public GameObject bullet;
    20.     AudioManager audioManager;
    21.     List<Collider2D> inColliders = new List<Collider2D>();
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         audioManager = AudioManager.instance;
    27.         rb = GetComponent<Rigidbody2D>();
    28.         anim = GetComponent<Animator>();
    29.         //make sure it always false, not dead
    30.         anim.SetBool("isDead", false);
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void FixedUpdate()
    35.     {
    36.         movement();
    37.         grounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, whatisGround);
    38.     }
    39.     void Update()
    40.     {
    41.         GetInputMovement();
    42.         newJump();
    43.         SwitchAnim();
    44.         if (Input.GetButtonDown("Press"))
    45.         {
    46.             inColliders.ForEach(n => n.SendMessage("Use", SendMessageOptions.DontRequireReceiver));
    47.         }
    48.     }
    49.     void GetInputMovement()
    50.     {
    51.  
    52.         if (Input.GetButtonDown("Fire1"))
    53.         {
    54.             GameObject mBullet = Instantiate(bullet, muzzle.position, muzzle.rotation);
    55.  
    56.             //audioManager.PlaySound("Shoot Sound");
    57.  
    58.             mBullet.transform.parent = GameObject.Find("GameManager").transform;
    59.  
    60.             mBullet.GetComponent<Renderer>().sortingLayerName = "player";
    61.             anim.SetBool("isShooting", true);
    62.         }
    63.         /*if (Input.GetButtonUp("fire"))
    64.         {
    65.             anim.SetBool("isShooting", false);
    66.             anim.SetBool("isRunning_Shooting", false);
    67.         }
    68.         if (Input.GetButtonDown("Fire1") && GetComponent<Rigidbody2D>().velocity.x>0)
    69.         {
    70.             anim.SetBool("isRunning_Shooting", true);
    71.         }*/
    72.     }
    73.     void movement()
    74.     {
    75.         float movehorizontal = Input.GetAxis("Horizontal");
    76.         float facedirection = Input.GetAxisRaw("Horizontal");
    77.  
    78.         if (movehorizontal != 0)
    79.         {
    80.             rb.velocity = new Vector2(movehorizontal * speed * Time.fixedDeltaTime, rb.velocity.y);
    81.             anim.SetFloat("running", Mathf.Abs(movehorizontal));
    82.         }
    83.         else if (movehorizontal == 0)
    84.         {
    85.             anim.SetFloat("running", 0);
    86.         }
    87.         if (facedirection != 0)// face left or right
    88.         {
    89.             transform.localScale = new Vector3(facedirection, 1, 1);
    90.         }
    91.     }
    92.     void newJump()
    93.     {
    94.         anim.SetBool("isJumping", false);
    95.         if (grounded)
    96.         {
    97.             extraJump = 1;
    98.         }
    99.         if (Input.GetButtonDown("Jump") && extraJump > 0)
    100.         {  
    101.             rb.velocity = Vector2.up * JumpForce;
    102.             extraJump--;
    103.             anim.SetBool("isJumping", true);
    104.  
    105.         }
    106.         if (Input.GetButtonDown("Jump") && extraJump == 0 && grounded)
    107.         {
    108.             rb.velocity = Vector2.up * JumpForce;
    109.  
    110.             anim.SetBool("isJumping", true);
    111.  
    112.         }
    113.     }
    114.     void SwitchAnim()
    115.     {
    116.         //anim.SetBool("idle", false);
    117.         // when rigidbody going down no touching ground animation equal false
    118.         if (rb.velocity.y < 0.1f && !coll.IsTouchingLayers(whatisGround))
    119.         {
    120.             anim.SetBool("isFalling", true);
    121.         }
    122.         if (anim.GetBool("isJumping"))
    123.         {  //when the character goind down animation true
    124.             if (rb.velocity.y < 0)
    125.             {
    126.                 anim.SetBool("isJumping", false);
    127.                 anim.SetBool("isFalling", true);
    128.             }
    129.         }
    130.         else if (coll.IsTouchingLayers(whatisGround))
    131.         {
    132.             anim.SetBool("isFalling", false);
    133.  
    134.         }
    135.     }
    136.  
    137.     //called when something enters the trigger
    138.     void OnTriggerEnter2D(Collider2D col)
    139.     {//add the object to the list
    140.         inColliders.Add(col);
    141.     }
    142.     //called when something exits the trigger
    143.     void OnTriggerExit2D(Collider2D col)
    144.     {
    145.         //remove it from the list
    146.         inColliders.Remove(col);
    147.     }
    148.  
    149. }
    150.  
     
    Last edited: Feb 25, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Nobody here reads minds. How to report your problem productively in the Unity3D forums:

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

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Start by adding Debug.Log statements everywhere involved in these toggles, so you can easily step through what the code is actually doing. 9 times out of 10 the issue becomes immediately obvious after that.
     
    Kurt-Dekker likes this.
  4. hledward7

    hledward7

    Joined:
    Jul 9, 2019
    Posts:
    8
    ok.... i solve this already by using my own code by the way thank