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

Question bool only equling at true in one script but not the other

Discussion in 'Scripting' started by NintendoAsh12, Jun 3, 2023.

  1. NintendoAsh12

    NintendoAsh12

    Joined:
    Feb 15, 2021
    Posts:
    88
    so my fireball started hitting the player so I now need to have them hardcoded in to shoot left or right but my is facing right variable is only equaling true in the script or more likely defaulting to true I am still a noob when it comes to transferring variables PLZ HELP
    Receiving code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. public class Weapon : MonoBehaviour
    6. {
    7.     public Transform firePoint;
    8.     public GameObject shotPrefab;
    9.     bool IsFacingRight;
    10.     public JohnAndJamesController player;
    11.     float rotation;
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         player.isFacingRight = IsFacingRight;//right here is where the transfer is happening
    16.         Keyboard kb = InputSystem.GetDevice<Keyboard>();
    17.         if (kb.kKey.wasPressedThisFrame)
    18.         {
    19.             Shoot();
    20.         }
    21.  
    22.         if (SimpleInput.GetButtonDown("Fire1"))
    23.         {
    24.             Shoot();
    25.         }
    26.  
    27.         if (IsFacingRight = true)
    28.     {
    29.         // Set the velocity to move left
    30.         rotation = 0f;
    31.     }
    32.     else if (IsFacingRight = false)
    33.     {
    34.         // Set the velocity to move right
    35.         rotation = 180f;
    36.     }
    37.     }
    38.     void Shoot()
    39.     {
    40.         Instantiate(shotPrefab, firePoint.position,  Quaternion.Euler(0f, rotation, 0f));
    41.     }
    42. }
    43.  
    the sending part of my movement code for is facing right:

    [code=CSharp][HideInInspector] public bool isFacingRight = true;

    void Left()//Left foot, let's stomp Cha Cha real smooth
    {
    MoveButton = -1;//sets move button to -1
    if (MoveButton == -1 && isFacingRight)
    {
    isFacingRight = false;//sets isFacingRight to false
    // ... flip the player.
    Flip();
    }
    }

    void Right()//Right foot, let stomp Cha real smooth
    {
    MoveButton = 1;//sets move button to 1
    if (MoveButton == 1 && !isFacingRight)
    {
    // ... flip the player.

    Flip();
    isFacingRight = true;//sets isFacingRight to true
    }
    }
    [/code]

    the sending part is just several snippets of one long script that mostly has nothing to do with left and right
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Those are two different variables, completely unrelated.

    Keep one variable in one place, probably on the player since "is facing right" refers to the player.

    Then the other script references that variable in the player. Here's how:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Your code appears to want to read the value out of
    player.isFacingRight
    into the Weapon's
    IsFacingRight
    to determine rotation. If that's the case the line above is in reverse and should instead be:
    Code (csharp):
    1. IsFacingRight = player.isFacingRight;