All posts by B

Resetting Answer object and moving it back – finished in “Resetting game state”

This post became too much of a hot mess, so I put the solution in “Resetting game state” at http://girlcodeitblog.com/2016/04/05/resetting-game-state/.   (#todo – why are urls not turning into links … )

But in the interest of saving my work, here’s my original thoughts on it:

I just moved an object onto a collider, everything else happened great, but it’s not moving itself back when I tell it to.

Tried this first:

transform.position = new Vector3(1.84f, 4.43f, 10f); // this is what I tried first and it didnt work

Why no worky?  Tried this.  Still no worky.

 gameObject.transform.position = new Vector3(1.84f, 4.43f, 10f);

So it turns out that the script on this object that allows it to be dragged and dropped by a mouse was the problem.  It was still moveable, and so it wouldn’t change its transform? Don’t quite understand.  But when I add this, suddenly it works:

            MoveObject otherScript = gameObject.GetComponent<MoveObject>();
//                MoveObject answerScript = GameObject.Find(Answer).AddComponent<MoveObject>();
            Destroy(otherScript);

 gameObject.transform.position = new Vector3(1.84f, 4.43f, 10f);

Now the trick is is make this object invisible again (it’s not supposed to be visible during gameplay, only as an answer), and add the MoveObject script back onto it so that when it becomes visible it can be dragged and dropped into the correct answer bin.

What I’d LIKE to do is deactivate it like this:

gameObject.SetActive(false);

However, when I do that, the rest of the game stops working, even when I set it back to active in the other script Start().  Sooo I’ll leave that for another day and just keep transforming the scale to Vector3(0,0,0).  Feels like a hack, but it sure works! (Got it from : http://answers.unity3d.com/questions/7776/how-to-make-an-gameobject-invisible-and-disappeare.html . Want to further explore the pros and cons of each option, but in the meantime I’m using some mix of this and SetActive(false), depending on which one works easily in context.)

gameObject.transform.localScale = new Vector3(0, 0, 0);

Now I just have to add my moveable script back onto the object after it’s been de-scaled.  Note – I tried to see if I could get away without deleting the script because I’d transformed the position and also the scale – turns out it puts it back in the spot I left it in?  Don’t know why.  Actually instead of messing with the MoveObject script, I’m going to see if setting the transform.position in the other script that calls it in its Start() function works.  Seems … cleaner somehow.

GameObject.Find(Answer).transform.position = new Vector3(1.84f, 4.43f, 10f); 

Gosh no that doesnt’ work at all.  Apparently I can’t descale that object while MoveObject script is on it.  Okedoke.  Someday I’ll understand why but in the meantime I’ll make it work.

Now I’ve got this:

 

            MoveObject otherScript = gameObject.GetComponent<MoveObject>();
////                MoveObject answerScript = GameObject.Find(“Answer”).AddComponent<MoveObject>();
            Destroy(otherScript);
//
            // move Answer back to original transform

            gameObject.transform.localScale = new Vector3(0, 0, 0);
            gameObject.transform.position = new Vector3(1.84f, 4.43f, 10f); // this is what I tried first and it didnt work
//            gameObject.SetActive(false);
            // destroyed audio sources  easy peasy 
//            MoveObject otherScript = AssetDatabase.LoadAssetAtPath(Assets/Scripts/MoveObject, typeof(MoveObject)); // apparently I don’t need this part because it’s already been defined, even tho it just got destroyed …
            otherScript = gameObject.AddComponent<MoveObject>();

Okay. So now the Answer object isn’t reappearing.  Messed sthing up somewhere.  It works for half of my objects but not the other half.  This doesn’t make much sense so I must’ve really done sthing fun in there.

I didn’t set it to the right z location, it was behind the background.

 

WRONG:

GameObject.Find(Answer).transform.localScale = new Vector3(0.08f, 0.08f, 0);

 

RIGHT:

GameObject.Find(Answer).transform.localScale = new Vector3(0.08f, 0.08f, 10);

And also apparently it’s not always picking up on the audio components of other objects.  It works for some, but not others.  I’m going to walk away from this soon and let everything marinate.  I think I’ll be able to solve it tomorrow.

Reset an object to the prefab state

I’ve got an object that gets filled up with new components during gameplay, particularly audio components.  Do I delete the one I just used and instantiate a prefab, or do I delete the audio components off of the object I’ve been using?  I tried both ways, and I’m sure there are ways to get both to work, but the one that worked better was deleting audio components off the object.  This is because that object was referenced in other scripts’ public variables through the GUI drag&drop stuff, and if I deleted that object outright, suddenly those scripts weren’t referencing the object anymore.

Here’s the script for getting all audiosources and destroying.  (Based on stuff from http://answers.unity3d.com/questions/275714/how-to-find-all-components-of-a-game-object.html)

            // destroyed audio sources  easy peasy 
            Component[] audioSources = gameObject.GetComponents<AudioSource>();
            foreach (Component audiosource in audioSources

{
                Destroy(audiosource);
            }

<— code block isn’t working there.  Probably don’t have my SmartSyntax set up right?  Gonna switch to GitHub Gist when I have the bandwidth. #todo

Here’s the script for instantiating the prefab (based on http://forum.unity3d.com/threads/editor-script-create-game-object-from-a-prefab.47845/).  Didn’t wind up keeping this one, but it’s nice to have on file.  Also I’ve had trouble loading assets at the path (before I’ve used Resources.Load, and am now wondering which one is better for my purposes – I’ll check that out in a later post! #todo).

            Object prefab = AssetDatabase.LoadAssetAtPath(Assets/Prefabs/Answer, typeof(GameObject));
            GameObject answer = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;

Finding a good host for a beginner’s programming blog

Hi!  I wasn’t expecting to have my first problem-solving post be about the blog itself, but this ended up being more time-consuming than I’d thought, so I’m gonna go meta. Here’s what I ended up doing.

I used WordPress through BlueHost. This cost money (I paid sthing like $71.95 for it – unclear if I needed all the things that it added on to it, but I was feeling really impatient and wanted to get things on a roll.  If I didn’t need the extras, please say in comments!).  Reason why I went for BlueHost: WordPress.com doesn’t allow you to add plugins (as far as I can tell/ google tells me), so I needed an easy hosting system that could also install plugins.  Why plugins?  Because I want code blocks to not be ugly.  Why not do it all myself?  I’ll do that soon!  Right now, one learning topic at a time – and the current topic is Unity 3D/C#, so I can get some fun indie games up and running asap.

Heads-up – BlueHost takes what feels like forever to set up (probably about 45 mins-60 mins in reality), and looks like it’s having an error or a firewall problem while it’s setting up.  The website assures you it will take “a few minutes”, but when I waited 30 mins to talk to one of their live chat folks, she told me it could take as much as 4 hours.  Oof!  You get an email when it’s working, and right after that mine started, so that was happy!  Set your expectations to not being able to play with your new website immediately, and don’t waste time like I did, and you’ll be a happier blogger.

Then I went to the sidebar, plugins, selected “add new,” and searched for “code block.”  I picked Smart Syntax because I’d been googling around and saw that Google prettify was good, and Smart Syntax uses that, so I figured I’d start with that.  The others also looked fine and I’d seen references to those as well.

Test run of code block:
    public void FillDictionary()
    {
        WordDict.Add(gug, Trash);
        WordDict.Add(hug, Notebook);
        WordDict.Add(bug, Notebook);
        WordDict.Add(plug, Notebook);
        WordDict.Add(wug, Trash);
    }

Code block is looking good!  Unclear if it was Smart Syntax that did that or if cut and paste just works well in this editor.  I’ll figure that out later, but for now am satisfied that this appears to be working.  Haven’t figured out fonts yet, and would love to make everything, especially the code, a smaller font.

Other positive thing – it looks like it’s autosaving every few minutes (little bar at the bottom saying wordcount and time when draft was saved).  Time is set to … 4 hours later than it is.  I’ll also fix that later, but I’m thrilled that this appears to autosave.  In my earlier (read: 2 hours ago) forays into SquareSpace and WordPress.com, I lost some of my initial blurbs, and found the Save button frustrating because it would take me out of the editor.  (There’s a decent chance I was doing something wrong, but I’d rather have a nicer UI system.)

Slightly negative thing – it feels pretty slow navigating around different parts of this site.  Worth it for now, but I want to keep looking for other options.