#Script Reference
1 messages · Page 1 of 1 (latest)
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..
ok go on...
I'm trying to use the child button to make the script reference from its parent specifically
so the button's parent has the script attached to it
and you just want to access that script?
GetComponentInParent<MyScript>()
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}");
}
why are you looping over all of them?
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)
lmao yes for 2 days I've been trying to solve this , when I use the get index method it gives a wrong index always ... -1 to all 4 buttons
well then let's fix that problem 😉
at this point I'm ready to bang my head against the wall
I have tried this method
https://answers.unity.com/questions/1614589/make-a-list-of-buttons-produce-their-index-number.html
and this method
https://stackoverflow.com/questions/49317488/how-i-can-get-gameobject-index-in-the-list
among others
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
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
ok looks good, right?
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
well how is that set
you're still doing the loop thing, no?
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>();```
It should be set in the second function right
yes
private void OnKeyPressed(int keyIndex)
{
UnlockBackgroundButton(keyIndex);
}```
something like this
if I understand the situation correctly
private void OnKeyPressed(int keyIndex)
{
unlockablesInfo = gameBackgroundButtons[keyIndex].GetComponent<PlayerUnlockables>();
Debug.Log($"Index: {keyIndex}");
}
now clicking a button does nothing
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
wdym it stopped it
the only way that would stop it is if there's an error
so is there an error?
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
this isn't printing anymore?
Did you remember to reassign the buttons to call this function?
now that we added a parameter
I get that console Log I made ... because the script reference is null
if this log isn't printing that's the issue you need to focus on
Omg i think its working
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?
I'm actually confused now because I thought you were assigning the on click in code
instead of doing it manually
you shouldn't have to assign anything manually
isn't that what this loop does?
I guess not I had to change it maanually
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?
Recorded with my phone
Here's a working video , sorry my pc seems yo be temperamental on screen recording
Bottom of the inspector , looking at unlockable info
you have two errors btw
in your console
also it looks to be working fine, no?
Yes one is for my leaderboards and the other is for my notifications
Yes seems to be functioning correctly now and seems pretty scalable ( if I wanted to add to them in the future )