#https://gdl.space/ekipahabob.cpp i am
1 messages · Page 1 of 1 (latest)
well i tried the scenedata which you suggested, but it still nothgn changes
do you know how to use a scriptable object
well no, this is my first time
ok so this is gonna be a little more complicated
ill explain it but lemme show you the easy way first
public class LevelManager
{
public static int level1MuonCount;
public static int level2MuonCount;
}
something like that would be a lot simpler
[CreateAssetMenu(fileName = "SceneData", menuName = "ScriptableObjects/SceneData")]
public class LevelData : ScriptableObject
{
[field: SerializeField] public string SceneName { get; set; }
[field: SerializeField] public int MuonCount { get; set; }
}
public class LevelManager : MonoBehaviour
{
[SerializeField] private LevelData level1Data;
[SerializeField] private LevelData level2Data;
private Dictionary<int, LevelData> _levelDataMap;
private void Awake()
{
_levelDataMap = new()
{
{1, level1Data},
{2, level2Data}
}
}
public int GetMuonCount(int levelIndex)
{
return _levelDataMap[levelIndex].MuonCount;
}
public string GetSceneName(int levelIndex)
{
return _levelDataMap[levelIndex].SceneName;
}
}
i mightve spelled some things wrong im typing this in discord
in the editor you right click like youre adding an item and at the top of the menu you should a new ScriptableObjects section
OK, i hope
there sure is some red underlines
also the two different scenes are calles "Stage 1-1" and "Stage 1-2"
you need to add the usings
like System.Collections.Generic
UnityEngine
maybe there are some spelling errors since i didnt write it in an ide
but im sure youcan figure that out
under new
[CreateAssetMenu(fileName = "SceneData", menuName = "ScriptableObjects/SceneData")]
public class LevelData : ScriptableObject
{
[field: SerializeField] public string SceneName { get; set; }
[field: SerializeField] public int MuonCount { get; set; }
}
public class LevelManager : MonoBehaviour
{
[SerializeField] private LevelData level1Data;
[SerializeField] private LevelData level2Data;
public Dictionary<int, LevelData> Map { get; private set; };
private void Awake()
{
Map = new()
{
{1, level1Data},
{2, level2Data}
};
}
}
you can make the dictionary static if you want and in your other script where you need the count or whatever you can do LevelManager.Map[1].MuonCount
new() is a newer feature idk what version youre on but maybe you can try
Map = new Dictionary<int, LevelData>()
there seems to be still a red uenrline on new
oh wait, i accidentally put it in the wrong one
still having these red underlines
do this
remove that semicolon
Map = new Dictionary<int, LevelData>()
{
{1, level1Data},
{2, level2Data}
};
im going to bed. what i wrote should work after fix that. after that you need to look up hwo to use scriptable objects
or jsut do this
Every time there is a red underline, you of course need to share what the error is. But perhaps it has something to do with the syntax error on the line where you declare Map
_levelDataMap instead?
No, you just have to remove the semicolon.... the thing that is underlined
I drew a red arrow pointing right to it
oh
we.... i am on this one, even if i uses that, it still doesn't changes because it still stays on "0" evne it's supposed to coung how many "muon" objects there are in other scenes
and I don't get how any of it have to do with MuonCounter script...
Ah, they made the syntax error then. Yeah, autoproperties do not have a semicolon at the end
Only inside the curly braces, after get and set
Also, you still need to show what the error actually says
Just showing a red line is not usually helpful
well, the third image shows something
You changed it back to Map = new() for some reason
You need the full type, as you had here
#1265474997183320204 message
Well, that is a reference, so you need to drag the scene assets into those references via the inspector I guess
what am i doing wrong?
did you fix it
I can't drag in anything in the scene data inspector
if you know what i mean
i dont
in the inspector about none (scene data) what am i supposed to add in there?
did you create instances of the scriptable objects
instances?
did you read about scriptable objects
you need to create an instance of them to use it
In this video, we are going to show you how to use Scriptable Objects to manage your data in Unity.
Download the Unity Royale Project here!
https://on.unity.com/33FplpG
Learn more about Scriptable Objects here!
https://on.unity.com/33PzcJR
Get Inspiration on what else to do with Scriptable Objects here!
https://on.unity.com/2RKXXkK
Chapters:...
instantiate?
watch the video
I am
1:30
ok
so did you create the instance
you right click in the editor hierarchy like youre creating an asset and go to ScriptableObjects -> SceneData
the video explains it
you just add your data to that then drag it over into the level manager
yet what does have any of it to do with showing how many muons there are from other scenes while it is still not showing how many there are
I just don't get it... i am confused...
this is just for storing the counts
to read the counts you will access the level manager
on your object that you want to show the count you can do something like LevelManager.Map[1].MuonCount
you can also use it for loading scenes like SceneManager.LoadScene(LevelManager.Map[1].SceneName)
then you can add more stuff to LevelData if you need ot access it outside the scene
in which script?
do your objects that display the count have a script attached
the muon objects only have the script on how htye work when you touch them
i mean in this scene
those icons that show the count
it's jsut the text
the second 0 is supposed to show how many muons there are in other scenes
ok so in your LevelManager you can do
private int GetTotalMuonCount()
{
int count = 0;
foreach (var item in Map)
{
count += Map[item.Key].MuonCount;
}
return count;
}
yup
then in the script you want to display the count you just do PlayerManager.GetTotalMuonCount()
but doesn't it get a red underline there if i put it in there, in the same script?
oh if its in the same script then just do GetTotalMuonCount()
these things you can fix yourself
just read what the error is
im trying to help but im not sure if youre just really confused or you dont wanna help yourself
do you understand what the error says
yes, it sys that the method must have a return type
the pictures youre posting aren helpful
do you have a function called PlayerManager?
the script is called PlayerManager
can you just show the code
thats not how you call a function
you need to call it inside another function definition or property
if youre using textmeshpro you use get a reference to the label
like [SerializeField] private TextMeshProUGUI countLabel;
then you drag the label into the playermanager field for that
then in start you can do countLabel.text = GetTotalMuonCount().ToString()
in your start method i mean
private void start?
yup
i would recommend you try learning a little bit more about c#. its gonna make your development a lot faster
We have been over this exact issue multiple times before linedol. You cannot call a method outside of a method body
i seem to have gotten an error
i have no idea whats at line 297
Line 297 of PlayerManager
maybe its because you didnt add another reference for the second level
Ah yes ðŸ§
The value at that key is null
ok, now it's no logner showing the error, but yet.... it still doesn't showing the amount of muons there....
you need to add the data for level 2
I did
did you edit the data
its not gonna auto update without you writing it in. if you want it to update automatically you need to make an editor script
even if i did, it still wouldn't show any numbers
did you do this
yes
can you show the code
and where the countlabel is
also, it's supposed to show how many muons from each scene there is, not to choose a number
https://gdl.space/oruvuroyag.cs and this is the script the UItext have
this seems like its overwriting
also
youre not gonna be able to easily just get the count from each scene at runtime so easily
youd probably have to open each scene to get the count
you dont need to write it in yourself. you can make an editor script to automatically do it for you
but thats more advanced and if youre having trouble with this i dont think its smart to go into details about editor scripting
but that is not how i wanted it to work, because even in game, you only start a the title screen, then to the menu screen, then to a world select screen, in the level select screen and stage selection screens where it shows how many muons there are from each stage scene and how many of them you have rescued...
i told you before that this is for a static count
I know...
yet even though i have static, i don't get why it is still not working...
no
static meaning it wont change
meaning this solution works if the count doesnt change in your game
i also told you that if it changes you will need to implement some data persistence
ok so ill ask this differently
you want to have a count of all muons in game, and another count of the ones you saved as "4/10" for example?
yeah, and while currently, there are 3 muons in each stage, and there's 5 stages (while on is a travel stage) and the one big stage is a boss stage, which makes 15 together
and yeah, like when you rescued all muons in one stage, which makes it "3/15"
saved
so you don't have to start over and over agian then that would be fustrating to rescue a lot of the muons agian
ok so weve spent a lot of time on this so im just gonna give you a general direction
first watch this https://www.youtube.com/watch?v=aUi9aijvpgs
In this video, I show how to make a Save and Load system in Unity that will work for any type of game. We'll save the data to a file in both a JSON format as well as an encrypted format which we'll be able to toggle between in the Unity inspector.
IMPORTANT: For WebGL games, because WebGL can't save directly to a file system, a different meth...
then you can use what i gave you to keep track of the muons or delete it. it is useful if you want to add more data about your levels later on but you might not need it
you will store a count of the muons in your game, using the system i showed you or just a simple int muonCount = 15
youll need a variable like muonsSaved = 0
when you save a muon you do muonsSaved += 1
then you will use the save system from that video to store the save count even after you close the game
https://gdl.space/sagayohiwi.cs well i have this
ok good
so just store the save coutn there
now the other issue is your count not updating in game
this seems to be whats controlling the text label
so in this script you just need to fetch the muon count and save count and update it
https://gdl.space/hudoyehowa.cs and of course this one is counting hte muons in the world select and stage select scenes from the stage scenes
fetch the muon count? like putting it in the persistence script?
in the persistence you will get the muon remaining count
pass that to the script that controls the label
at the start of that level change. while youre playing you dont need to access the persistence script if its just gonna read the file again. you can store that in memory
..i... just don't know what to write
in the persistence script when the game loads you get the muon remaining count
you save that in memory
make a private int muonsRemaining then muonsRemaining = (get it from the file or whatever method youre using)
you can make it a public property actually
then whenever you go back to that level select scene you can read the property to update the count
https://gdl.space/dagetejaco.cpp i do have another one, when in the stage games... while collecting coins or something
ok
try that and if youre confused you can ask for more help
but first try to figure out the steps that i outlined for you
what's the difference between loadgame and savegame?
you save a game when you wanna go back to that point
like if youre gonna clsoe the game or reach a checkpoint you save it
then load takes you back to that saved state
and don't forget hte muon counter which is used for the ui text?
https://gdl.space/osovehamok.cpp
https://gdl.space/edogekutur.cs
I don't know what I'm doing is right...
I am getting anxious about it...
and what does this have to do with the current situation I'm in?
https://gdl.space/hanutixoya.cs you know, imma just gonna do this instead....
i was asking because scriptable objects arent for saving data
oh,well... https://gdl.space/kimahokafa.cs the script is now currently this
is it working?
well.......... I don't get how TotalMuonPerLevel and CurrentMuonsPerLevel texts can tell with only just that?
since the number on the first number text still wouldn't go up when i touched a muon gameobject
when you pick it up are you calling PickedUp()
well yeah the pickedUpwould be when i touch it
yes
foreach (string tag in muonTags)
{
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(tag);
_allMuons += gameObjects.Length;
}
this is only gonna be called on start
so this only works when you load the level screen
is your text object always in the scene
only in the level select scene
then PickedUp() is going to throw an error
whats the issue youre having now
do you know if pickedup is actally being run
oh i guess it wont because updatetext checks for null
so if PickedUp works, when you go back to the level screen the value should still be correct
you need to call UpdateText when you load the level select scene again
the number should change up number hwne i come back to the level screen, yeah
I was told that I can Now store the total and current for each level. Then I can access these dictionaries from anywhere to change the number when I collide with one, or to display as text would access this object and change the values on it whenever you collide with one, but yet i don't know how that would with only just
Dictionary<string, int> TotalMuonsPerLevel = new Dictionary<string, int>();
Dictionary<string, int> CurrentMuonsPerLevel = new Dictionary<string, int>();
so should i call it in DataPersistance script? or the counter script?
on the counter script
oh alright, and it should have private MuonCounter muonCounter;
the way you have it written you cant access the dictionaries anywhere
youre defining them in the start method
so they only exist in that method
anyway thats not the issue
you need to call UpdateText when you load the level select scene
you can do that in the counter scripts start method
you know ive told you and other people told you when you just post code with a red line its not helpful
did you read the error
im sure its because UpdateText is a method
it should be UpdateText()
I was only showing if it's there i should put it?
change it to public
ok, there
see if it works
it seems not to be working, and i got two errors when i enter the level stage scene
you changed the script so you have to post the code again
did you read the errors
i dont think youre gonna learn anything if you just ask for help at every problem
hnag on, i am on the way
ever sine i add in the updatetext, i seems to be getting these two errors
private MuonCounter muonCounter = null;?
no
the problem is that its null during runtiem so you cant access UpdateText
so setting it to null is not gonna do anything
its already null
you need a reference to the counter
did you set it
should i reference the counter in counter?
tbh i dont know whats going on
you posted 1 line of the updated code
you make it so hard to help you
and no offense but tbh im almost not feeling like it anymore because it doesnt seem like youre trying to help yourself and the errors you are posting arent helpful
let me explain something
before the updatetext was added in the coutner, there weren't any errors, but when i added i nthere, i got errors, even though the errors i get have nothing to do with the muoncounter since it's just for the coins
when you write private MuonCoutner muonCounter this is just stating that there is a variable of that type and name
when you do this its gonna be null
so you need to set it to something before you try to access it
so before you call UpdateText you need to initialize muonCounter
do you understand
like this?
no more errors, but yet the number still won't go up when i touched a muon object
in PickedUp but a debug.log to see if its actually being called
pick up doesn't seem to trigger
im confused
and not only that, number go up seems to be triggering when i start the scene
you have a class called Counter and MuonCounter and both have a method called PickedUp
the pickup in Counter is for the coins
where is your collision function
collission function?, the counters don't have oncollisions
how are you picking them
there should be a function that is called
either oncollision or ontrigger
the Muoncounter is for countin how many gamoebject muons you have touched, and muons already have their own script
yea i get that
how is the counter gonna know something is picked up
you have a pickedup method
when the collision happens you should let the counter know or set up an event
otherwise the counter is never gonna know to update
yeah, wait, was it supposed to be pickedup() instead of updatetext)(?
what
because i just tested that one and the number is finally going up when i touched a muon object
yeah... almost, since when i rescued 3 of the muons in the level scene, the number have only gone up one time, staying at "1/15"
what did you change
https://gdl.space/onaxuhotuw.cpp
https://gdl.space/defijekimo.cs
well the only thing i changed is update text to picked up in counter
ok
read this
then read the start method again
and try to figure out why that happened
hmmm.... when i play tested, the only thing that is making the number go up is the first muon object with tag name "Muon1"
also i am doing the reading and trying to figure out
well here's the debug when i enter the scene, also bu don't pick anything up.. you mean do not pick any muon objects up?
well nothing happens to the counter
oh wait, a number 1 went up
do you know why
follow the code from the counters start method
start is called when the gameobject is set to active, after spawning
it's because it went up of when finishing the level
im gonna tell you this but next time you need to read it yourself
when the counter spawns unity calls the start method
in your start method you are calling muonCounter.PickedUp()
in PickedUp you are increasing the coutn
so everytime this counter is loaded youre increasing the coutn
ohh....
yeah
ok so change it back to what i told you earlier
ok, i have chnaged it back
start should call UpdateText everytime its loaded, not PickedUp
ok
so we know that PickedUp at least kinda works
back to what i said before
you need to call PickedUp when the collision happens (when youre actually picking it up)
so like... oncollissionenter?
yeah
this isnt what i would say is the best way to do it but it should work
lets just keep it simple
i always though collission is somehting you touch
ok
it is
im not sure if this is what youre thinking but youre not gonna put the collision method in the counter
no
you can put it in your player script or the muon script
assuming your muon script is the one detecting the collision you would put it there
https://gdl.space/deyomuveto.cs the muon scipt is with the muonobject which you touches after all
in OnTriggerEnter2D
https://gdl.space/elayiqetex.cs, ok there
no
remember what i told you
private MuonCounter muonCounter; is just a declaration
you need to initialize it or youll get a null error
also, you have some checks there on what the collision object is, so you should add picked up inside that
man my game sure is going to be abig project..
this is the easy part
dont get overwhelmed
the problem is you dont know much about how code works
youll learn while building it
first try and second try, the first try when i moved the player around in the scene tab and second try was when i tried in the game tab
so it works?
yeah, i hope so, and now to save the data when rescuing them
thank you, now for saving the data on about the text like when you exit the game and play it agian
uhhhhh..... ok, now this is a problem right now that sometimes number go by 2 when i touch one muon game object
post the collision code then im going to bed
ok
private void OnTriggerEnter2D(Collider2D collision)
{
muonCounter = FindObjectOfType<MuonCounter>();
if (muonCounter == null)
{
Debug.LogError("MuonCounter not being found");
}
else
{
muonCounter.PickedUp();
}
if (collision.CompareTag("Player") || collision.CompareTag("Player 2") || collision.CompareTag("Attack") && !isRescued)
{
isRescued = true;
animator.SetTrigger("Rescued");
animator.SetBool("isRescued", true);
popSound.Play();
// Start a coroutine to handle the happy animation and disappearance
StartCoroutine(HappyAnimationAndDisappear());
}
}
you didnt do this
probably the collision is happening twice
sometimes and sometimes not, even sometimes 3 times... i just nee dit to happen once
based on what gameobject it is
in the if statement you set isrescued to true
part of the if clause is if its not already rescued
so if you add the pickedup inside the if clause it should prevent it happening twice
thats the whole point of what you wrote
like this?
yup
ok, thank you, man my game sure is going to be big, even with 3 save files to choose from