-2

Hello I have 2 script my recoil script and my gunystem script

I have my recoil script on my camera recoil obj as a child of camera rot obj

I have my gunsystem script on multiple guns

i am trying to make all guns have different recoil

how do i do this I am new to C# and unity and this is my first game

Where the recoil Script is placed

My Recoil Script: Note: Just a part of the script

using UnityEngine;

public class Recoil : MonoBehaviour
{


    //Rotations
    private Vector3 currentRotation;
    private Vector3 targetRotation;

    //Hipfire Recoil
    public float recoilX;
    public float recoilY;
    public float recoilZ;

    //Settings
    public float snappiness;
    public float returnSpeed;

    void Start()
    {

    }













My GunSystem Script: Note: Just a part of the script

using UnityEngine;
using TMPro;

public class GunSystem : MonoBehaviour
{
    //Gun stats
    public int damage;
    public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
    public int magazineSize, bulletsPerTap;
    public bool allowButtonHold;
    int bulletsLeft, bulletsShot;

    //bools 
    bool shooting, readyToShoot, reloading;

    //Reference
    public Camera fpsCam;
    public Transform attackPoint;
    public RaycastHit rayHit;
    public LayerMask whatIsEnemy;

    //Graphics
    public GameObject muzzleFlash, bulletHoleGraphic;
    public TextMeshProUGUI text;









I tried placing these variables in the gunsystem script:

 //Hipfire Recoil
    public float recoilX;
    public float recoilY;
    public float recoilZ;

    //Settings
    public float snappiness;
    public float returnSpeed;

and refrencing the script in the recoil script but i dont know how to refrence a script that is an different game obj and how to refrence multiple variables.

  • 1
    [Accessing Other Game Objects / Components](https://docs.unity3d.com/410/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html) Page is super old, but still valid. Make sure to change the language dropdown to C#. – hijinxbassist Nov 30 '22 at 22:53
  • 1
    I guess you didnt search so as this has been asked a lot of times before – BugFinder Nov 30 '22 at 23:42
  • 1
    Does this answer your question? [Unity - Accessing other scripts' variables - put properly](https://stackoverflow.com/questions/42099487/unity-accessing-other-scripts-variables-put-properly) – BugFinder Nov 30 '22 at 23:47
  • You should start thinking in terms of *objects* rather than scripts or files. – Ňɏssa Pøngjǣrdenlarp Dec 01 '22 at 02:38

1 Answers1

0
public class Gun : MonoBehaviour
{

    //Hipfire Recoil
    public static float recoilX = 10f;

}

Access variables like this:

public class Recoil : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        print(Gun.recoilX); // 10
    }
}
Leo
  • 369
  • 5