#Script Reference

1 messages · Page 1 of 1 (latest)

bright dew
#

I have 4 ui buttons each with the same script , the script holds a different gem unlock value for each set in the inspector only..

still karma
#

ok go on...

bright dew
#

I'm trying to use the child button to make the script reference from its parent specifically

still karma
#

so the button's parent has the script attached to it

#

and you just want to access that script?

still karma
#

GetComponentInParent<MyScript>()

bright dew
#

this might help you see what's going on .... I'm trying to set the reference using a for loop but it does it for all of them instead of getting the object who's child button i clicked

#
public void UnlockBackgroundButton()
    {
        for (int i = 0; i < gameBackgroundButtons.Count; i++)
        {
            unlockablesInfo = gameBackgroundButtons[i].GetComponent<PlayerUnlockables>();

            Debug.Log($"Index Object Name: {gameBackgroundButtons[i].gameObject.name}");

            Debug.Log($"Index Number: {i}");
        }
still karma
#

didn't we deal with this at some point? Give the button on click function a parameter. It can be the button's index, or the button itself (as a GameObject)

bright dew
still karma
bright dew
#

at this point I'm ready to bang my head against the wall

#
still karma
#

yes those work

#

well the first one does

#

idk what the second one is doing

bright dew
#

in my start method

for (int i = 0; i < gameBackgroundButtons.Count; ++i)
        {
            int keyIndex = i;

            if (gameBackgroundButtons[i] != null)
            {
                gameBackgroundButtons[i].onClick.AddListener(() => OnKeyPressed(keyIndex));
            }
        }
private void OnKeyPressed(int keyIndex)
    {
        Debug.Log($"Index: {keyIndex}");
    }
#

produced this in console

still karma
#

ok looks good, right?

bright dew
#

also I clicked on the first button ( index 0 ) and it set button 4 as the script reference ( bottom of the inspector )

#

unlockables info reference

still karma
#

you're still doing the loop thing, no?

bright dew
still karma
# bright dew yes the for loop

you shouldn't use a loop anymore there, you should do this:

public void UnlockBackgroundButton(int button)
{
   unlockablesInfo = gameBackgroundButtons[button].GetComponent<PlayerUnlockables>();```
bright dew
still karma
#

yes

#
private void OnKeyPressed(int keyIndex)
    {
        UnlockBackgroundButton(keyIndex);
    }```
#

something like this

#

if I understand the situation correctly

bright dew
#
private void OnKeyPressed(int keyIndex)
    {
        unlockablesInfo = gameBackgroundButtons[keyIndex].GetComponent<PlayerUnlockables>();

        Debug.Log($"Index: {keyIndex}");
    }
#

now clicking a button does nothing

still karma
#

is there an error?

#

What changed

#

you said it was logging before, no?

bright dew
#

yes it was logging but I think its because I don't have the reference set on start

#

this unlockablesInfo = gameBackgroundButtons[keyIndex].GetComponent<PlayerUnlockables>(); stopped it

still karma
#

wdym it stopped it

#

the only way that would stop it is if there's an error

#

so is there an error?

bright dew
#

I made the reference to the first button on start and clicking buttons 2,3 or 4 doesn't change the reference to those objects

#

no error

still karma
#

now that we added a parameter

bright dew
#

I get that console Log I made ... because the script reference is null

still karma
bright dew
#

child button set up

#

I set the int in the on click myself , and it seems to be switching correctly now

#

can I get that on click int value to set itself or only can change it manually?

still karma
#

instead of doing it manually

#

you shouldn't have to assign anything manually

still karma
bright dew
still karma
#

that doesn't make sense. Something's still fishy about your setup

#

what's the point of that loop then

#

is that array actually populated?

bright dew
#

Recorded with my phone

#

Bottom of the inspector , looking at unlockable info

still karma
#

in your console

#

also it looks to be working fine, no?

bright dew
#

Yes one is for my leaderboards and the other is for my notifications

bright dew