Hi! So I just tried to make something like a shop where you can customize the character and it seems to be working fine excepting from one thig... I want the default look of the player to be Orange with Grey trail so the first option in both panels and that's what I set to be... Or at least I think I set it... But as you can see every time I start the game, the character is green with a yellow trail... anyone any ideas?
#Player customize not working as intended...
1 messages · Page 1 of 1 (latest)
Use codeblocks to send code in a message!
To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.
For example:
```cs
Console.WriteLine("Hello World");
```
Produces:
Console.WriteLine("Hello World");
To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.
a powerful website for storing and sharing text and code snippets. completely free and open source.
well lets see your values in the Inspector for defaultPlayerColorToggle and defaultPlayerTrailColorToggle fields
or better yet all of the fields in the Inspector for this ShopManager class
Where is the default logic in your code?
Really in the Start() I tried to set current toggle for both to the default
first I tried setting it directly like : currentColorToggle = defaultPlayerColorToggle but it didn't work
So I also tried calling the OnColorToggleClicked()
The code is in the paste or no
Ye it is
And as I said, I also tried like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShopManager : MonoBehaviour
{
[SerializeField] private Toggle defaultPlayerColorToggle;
[SerializeField] private Toggle defaultPlayerTrailColorToggle;
[SerializeField] private Toggle[] colorsToggle;
[SerializeField] private Toggle[] trailColorToggle;
[SerializeField] private ToggleGroup colorToggleGroup;
[SerializeField] private ToggleGroup trailColorToggleGroup;
[SerializeField] private Image[] trailExamplesImages;
[SerializeField] private Image lastTrailExampleImage;
[SerializeField] private Image[] playerColorExamples;
[SerializeField] private Image lastPlayerColorImage;
private Toggle currentColorToggle;
private Toggle currentTrailColorToggle;
Toggles.PlayerTrailColor playerTrailColor;
Toggles.PlayerColor playerColor;
// Start is called before the first frame update
void Start()
{
currentColorToggle = defaultPlayerColorToggle;
currentTrailColorToggle = defaultPlayerTrailColorToggle;
}
But I got the same result
Is this toggle a UI element or something you coded yourself
Also try to debug log more or use the debugger
Just an UI
I'll try but I am not really sure where to look/ add logs exactly...
Work backwards. First try the most low level or direct way to set that color
Then move backwards up the call chain if that makes sense
I think I got it... but how can I be more direct than just setting it to a specific color in the start?
Keep working backwards until it stops working
So, I just debug a bit more and I found something... here in those lines :
public void OnColorToggleClicked(Toggle toggle)
{
currentColorToggle = toggle; // This one specifically
if (currentColorToggle.isOn)
{
playerColor = toggle.GetComponent<Toggles>().playerColor;
currentColorToggle.interactable = false;
ChangePlayerColor();
Debug.Log(currentColorToggle);
}
else
{
currentColorToggle.interactable = true;
}
}
public void OnTrailColorToggleClicked(Toggle toggle)
{
currentTrailColorToggle = toggle; // And this one
if (currentTrailColorToggle.isOn)
{
playerTrailColor = toggle.GetComponent<Toggles>().playerTrailColor;
currentTrailColorToggle.interactable = false;
ChangeTrailColor();
Debug.Log(currentTrailColorToggle);
}
else
{
currentTrailColorToggle.interactable = true;
}
}
Those seems to be the one that affect the current active Toggle, so I tried to comment them and just leave the start where I make the current toggle for both to the default one but I get an error... like :
NullReferenceException: Object reference not set to an instance of an object... In both if statements lines
<@&823670198958948373> <@&823670121133768714>
I think the problem happen in the Toggles class. not ShopManager.
Hi!... And sorry for the delay but I had some problems with the network all day... So I am that sure because the Toggles class only has some enums... :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Toggles : MonoBehaviour
{
public enum PlayerColor
{
Orange,
Yellow,
Blue,
Green,
Pink,
Grey,
None
}
public enum PlayerTrailColor
{
Grey,
Yellow,
Orange,
Green,
Pink,
Blue,
None
}
[SerializeField]
public enum LockState
{
Locked,
Unlocked
}
[SerializeField]
public enum BuyState
{
Available,
Unavailable,
Bought
}
public PlayerColor playerColor;
public PlayerTrailColor playerTrailColor;
public LockState lockState;
public BuyState buyState;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Why are the enums inside the toggle class
Cuz... This Toggle class is assigned to each toggle
But you could just have the enums not in a class
Yes... I could... but is it bad to keep them like this?
Or would it be better to have just them?
Yeah but I always look to improve things up so I'll take that into account, thanks
But still... anything relevant to my main question?
As you wish :
So to get it from beginning, I am trying to make a simple shop system where you can customize a little your character. As seen in the video there are 6 color options for the player (pencil) and the trail that he leaves behind. The toggles work fine, they change the color of the player and trail as expected but the problem comes with the default color. Your default color should be Orange Pencil ( first option out of those 6 ) and Grey Trail ( first as well )but for some reasons the default color is set to Green Pencil ( option 4 ) and Yellow Trail ( option 2 ). The thing is that I don't really understand why because everything seems to be pretty straight forward... For more context :
· I have that ShopManager and this is the main script that changes the color and the active toggle accordingly (I'll repost the link to it after this message)
· I also have a class Toggles that contains some enums indicating what changes will that specific toggle do and it's availability ( I'll post it here as well )
ShopManager :
https://paste.myst.rs/w8r5mrft
a powerful website for storing and sharing text and code snippets. completely free and open source.
Toggles :
public class Toggles : MonoBehaviour
{
public enum PlayerColor
{
Orange,
Yellow,
Blue,
Green,
Pink,
Grey,
None
}
public enum PlayerTrailColor
{
Grey,
Yellow,
Orange,
Green,
Pink,
Blue,
None
}
[SerializeField]
public enum LockState
{
Locked,
Unlocked
}
[SerializeField]
public enum BuyState
{
Available,
Unavailable,
Bought
}
public PlayerColor playerColor;
public PlayerTrailColor playerTrailColor;
public LockState lockState;
public BuyState buyState;
}
If you need any more details I'll be more than happy to give
I tried to build the same scene. From you script I found out it required a lot of manual input which highly chance that your problem happen here. Like the toggles script that must added to each toggle ui and set value independent for each of them. After that you need to assigned value to
[SerializeField] private Toggle defaultPlayerColorToggle;
[SerializeField] private Toggle defaultPlayerTrailColorToggle;
[SerializeField] private Toggle[] colorsToggle;
[SerializeField] private Toggle[] trailColorToggle;
[SerializeField] private ToggleGroup colorToggleGroup;
[SerializeField] private ToggleGroup trailColorToggleGroup;
[SerializeField] private Image[] trailExamplesImages;
[SerializeField] private Image lastTrailExampleImage;
[SerializeField] private Image[] playerColorExamples;
[SerializeField] private Image lastPlayerColorImage;
It has a chance that you assigned the wrong reference. Maybe in scene view you toggle you saw the name as tg1 tg2 tg3 but it was place in order of tg2 tg1 tg3 in scene.
If you want to use this script I recommend to recheck all assigned value in all object you created and each value you add to above variable. If can't found anything then try to create new scene and follow old method. Assign every value again from the beginning. If new scene don't have a problem that mean you made some wrong ref/value in old scene.
If it still not working then I recommend to change method from toggle to button. You no longer require toggles class. Just change sprite depend on the index where index are changed in event button.
[SerializeField] private int colorIndex;
[SerializeField] private int trailIndex;
[SerializeField] private Sprite[] playerColorExamples;
[SerializeField] private Sprite[] trailExamplesImages;
Another debug method is:
Switch position inside colorsToggle array.
Change defaultPlayerColorToggle.
Switch position inside playerColorExamples array.
Try it one by one, gl.