Leveling up – making levels from .txt

So now I’ve got each row representing a level, with columns for each type of info that the game needs to fill in the blanks as levels change.

I haven’t done levels before, so this is totally new for me.

Oh.  Upon reflection.  At this point I’m not changing any of the objects *in* the scene, or anything about the scene setup.  The changes I need are to the objects’ textmesh and audio components, and those happen automatically through scripts.  I definitely don’t want a new scene for each level, because I’m going to have dozens upon dozens.

http://docs.unity3d.com/Manual/CreatingScenes.html

So now I just need to reset when the level is over.

#todo – separate this into discussion of static variables vs. making a new object that can get called.

The easiest way to do this is to make a static variable that absolutely any script can call.  However, this is generally considered bad form, and can make things buggy.  Unclear the extent to which this matters in Unity as opposed to other scripting environments, but people get quite heated.

I want to know how to do it both the static way and the make-an-object-and-attach-it-to-the-main-camera way.  Static way:

    public static int levelCounter = 1; 

Then I can call it in other scripts by calling the other scripts, as before (#todo add link to other post)

    protected GameManager gameManager; 

And then just call it.

GameManager.levelCounter

Oh wait – this looks like it’s using GameManager, *not* gameManager.  I guess I don’t even need to declare that it has access to the other script, because it’s a static variable with accessibility to anything, at any time?

As always, the problem I’m getting:

NullReferenceException: Object reference not set to an instance of an object

Is because I destroyed objects and forgot they were actually quite needed.  Funny how many times one can make that mistake, and how long it takes to figure it out each time!  Getting faster tho.

I suspect that instead of going to the trouble of *not* destroying these objects and being in an endless loop within the same scene, I actually want to reload the scene.

using UnityEngine.SceneManagement;

SceneManager.GetActiveScene()

Note – a lot of the tutorials still say to use  Application.LoadLevel(Application.loadedLevel) or other such things.  That’s deprecated and will make Unity mad at you.  It’s actually kind of a fun exercise going through some of the tutorials [#todo – add which ones haven’t been updated yet – probably RogueLike?] 

Okay so this doesn’t appear to do anything.  That’s because in addition to finding the current scene, I actually have to load it [from here]:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

Wooooo!!!!  Now to consider if I should do static variable for levelCounter or some better way.  (#todo)

And also #todo – some sort of progress chart, reward thing at the end of a level, naming of level before you start, etc.

 

#todo – rewatch this (Unity RogueLike tutorial).  Now that I understand IEnumerators better, and need to do levels with scene resets, I’ll probably be able to both understand and apply stuff better.

 

 

 

 

 

#todo – add this to another post [and consider “finding” objects less?  ask someone what better form is ]

http://forum.unity3d.com/threads/object-reference-not-set-to-an-instance-of-an-object-c.226430/

“Georetro,

This is a common discussion here on the forum, but instead of using .Find, you could use FindGameObjectWithTag(), apparently this is much faster. It might not be if you don’t have a lot of objects, but if your scene has a lot, it might help a bit.

Cheers.”

One thought on “Leveling up – making levels from .txt”

Leave a Reply

Your email address will not be published. Required fields are marked *