Unable to find a good way to reference a GameObject from another script

I need help with getting a game object called “Slot4” in one script to work in this script. I cannot say much rn as I got to do something.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SniperScope : MonoBehaviour
{

    public GameObject playerCam;
    public GameObject sniperScope;
    public GameObject crossHair;
    public GameObject sniper;

    private EquippingScript script;

    [SerializeField]
    EquippingScript EquippingScript;

    int isScoped;

    float scoped;

    private void Start()
    {
        script = GetComponent<EquippingScript>();
        

    }

    void Update()
    {
        
    }

    private void ScopedIn()
    {
        if(Slot4 = true)
        {
            if (Input.GetMouseButtonDown(1))
            {
                playerCam.GetComponent<Camera>().fieldOfView = 30;
                sniperScope.SetActive(true);
                crossHair.SetActive(false);
            }
            if (Input.GetMouseButtonUp(1))
            {
                playerCam.GetComponent<Camera>().fieldOfView = 60;
                sniperScope.SetActive(false);
                crossHair.SetActive(true);
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(1))
            {
                playerCam.GetComponent<Camera>().fieldOfView = 60;
                sniperScope.SetActive(false);
                crossHair.SetActive(true);
            }
            if (Input.GetMouseButtonUp(1))
            {
                playerCam.GetComponent<Camera>().fieldOfView = 60;
                sniperScope.SetActive(false);
                crossHair.SetActive(true);
            }
        }
        
    }
}

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

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.

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: Regarding GameObject.Find · UnityTipsRedux

In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

“Stop playing ‘Where’s GameWaldo’ and drag it in already!”

Here’s why all this stuff is CRAZY code:

More information is needed. Where is “Slot4” with respect to this particular component and the game object it’s on? Child game object? Same scene? Different scene? Does it relate to this EquippingScript?

You’ll only get general advice like Kurt’s without being more specific.

Slot4 is a GameObject, I can send that script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EquippingScript : MonoBehaviour
{

    public GameObject Slot1;
    public GameObject Slot2;
    public GameObject Slot3; 
    public GameObject Slot4;
    public GameObject Slot5;

    private SniperScope pm;



    void Start()
    {
        
    }


    void Update()
    {
        if(Input.GetKeyDown("1"))
        {
            Equip1();
        }
        if (Input.GetKeyDown("2"))
        {
            Equip2();
        }
        if (Input.GetKeyDown("3"))
        {
            Equip3();
        }
        if (Input.GetKeyDown("4"))
        {
            Equip4();
        }
        if (Input.GetKeyDown("5"))
        {
            Equip5();
        }


        if (Input.GetKeyDown(KeyCode.JoystickButton3))
        {
            Equip1();
        }
        if (Input.GetKeyDown(KeyCode.JoystickButton2))
        {
            Equip2();
        }



    }
    void Equip1()
    {
        Slot1.SetActive(true);
        Slot2.SetActive(false);
        Slot3.SetActive(false);
        Slot4.SetActive(false);
        Slot5.SetActive(false);
    }
    void Equip2()
    {
        Slot1.SetActive(false);
        Slot2.SetActive(true);
        Slot3.SetActive(false);
        Slot4.SetActive(false);
        Slot5.SetActive(false);
    }
    void Equip3()
    {
        Slot1.SetActive(false);
        Slot2.SetActive(false);
        Slot3.SetActive(true);
        Slot4.SetActive(false);
        Slot5.SetActive(false);
    }
    void Equip4()
    {
        Slot1.SetActive(false);
        Slot2.SetActive(false);
        Slot3.SetActive(false);
        Slot4.SetActive(true);
        Slot5.SetActive(false);
    }
    void Equip5()
    {
        Slot1.SetActive(false);
        Slot2.SetActive(false);
        Slot3.SetActive(false);
        Slot4.SetActive(false);
        Slot5.SetActive(true);
    }
}

I have seen you say all that before in other things when I looked for a way to do it. I am not using find but I don’t know how to only do it for a GameObject and I do not need to know for variables.

If you already have a reference to EquppingScript, then it’s just script.Slot4, though these are not very good script/variables names.

You should also look to learn how to use collections such as arrays and List. Both accessing fields and using collections are basic C# things, so it’d be worth finding some courses on basic C# to get up to speed.

Well, now it is giving me an error on true. Cannot Implicitly convert type bool to Unity.Engine.GameObject, any idea why? I have done a bit of C# but have not done much with collections.

Can’t comment without seeing the code.

I have sent all of the code in a message above a little bit, the other script is in the og post.

You use == for comparisons, and = for assignments. You’re incorrectly using =.

You definitely need to do some basic C# courses.

I have done some, they just didn’t have anything on that, this is a learning experience for me. Still, what should I do for this part as true is giving me an error?

if(script.Slot4 = true)
{
    if (Input.GetMouseButtonDown(1))
    {
        playerCam.GetComponent<Camera>().fieldOfView = 30;
        sniperScope.SetActive(true);
        crossHair.SetActive(false);
    }
    if (Input.GetMouseButtonUp(1))
    {
        playerCam.GetComponent<Camera>().fieldOfView = 60;
        sniperScope.SetActive(false);
        crossHair.SetActive(true);
    }
}

I already explained what you’re doing wrong.

Ah, alright, thank you so much. I will check to see if this works. (Honestly, I just forgot == exists.)

Definitely take a moment to review your C# language famliarity… no sense in you confidently telling the compiler absolutely gibberish because it will absolutely do whatever gibberish you can get it to swallow. Mixing up = for == is one such example and there are many more.

The only defense is to become confident in antagonistically reading the code you wrote. Read it as if it was written by someone else and you have to fresh-understand it. I get into most of my trouble by failing to read an obvious typo I just wrote 30 seconds ago, squirreling around until I corral it down to the simplest test and attach the debugger and say “Wait a second, this value just BECAME true after I tested it, how is that possible?!”

Debug, debug, debug.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like

I am aware of debugging and use it constantly.