benArcen

Unity Questions Thread

Recommended Posts

Thanks TerriSchiavosGhost, your explanation was clear. It'll still take me some time to absorb. Based on what SpennyDubz suggested, I'm going to try to just ask myself "Am I assigning a new value to a component? Or am I assigning it to some float, int, bool, Vector, character, or string?" We will see if that heuristic takes care of it.

On a more serious note: Have y'all ever thought that maybe your new scenes are populated by tons of gameObjects, but you just don't know it because they lack the Transform to give them a location? Shit is deep.

Share this post


Link to post
Share on other sites

Why does the first one not work, but the second one does?

And how can I tell the difference in the future?

 

Regarding how to tell the difference in Unity specifically, try putting the following code in a C# file (I don't think my trick works with the "Javascript" files):

int i;
char c;
Vector3 v;
GameObject obj;
string s;

If you hover over the different data types (int, char, etc) you will get a little popup that tells you whether it's a struct or a class. It sounds like in Unity, structs are the "by value" types and classes are the "by reference" types, so you can tell roughly how they're going to behave based on that. =)

Share this post


Link to post
Share on other sites

At the risk of making things worse I will also indulge myself in explaining how I think about "by value" or "by reference" types, feel free to ignore if it's useless. =P

 

Every variable is either a piece of information you have jotted down to remember later, or an Actual Thing which you hold fast in your clutches so that you may manipulate it directly.

 

Person clyde = new Person(); //this is you; you is an Actual Person (in Unity, I guess it would be an actual GameObject)

int clydesAge = clyde.age;  //this is me asking how old you are, then scribbling it down on a piece of paper labeled "clydesAge"
clydesAge = 2;  //this is me crossing out what I wrote on the paper and writing "clydesAge is 2" -- it doesn't actually turn you into a baby!

Person myVictim = clyde;  //this is me getting voodoo hooks into your Actual Personhood, your VERY BEING!!!
myVictim.age = 2;  //this is me recklessly altering your true properties, thereby turning you into a TINY BABY
clyde.age = 150; //"clyde" is also a reference to your True Self, so now you are DANGEROUSLY ANCIENT

myVictim = new Person(); //this is me turning my fearsome powers on some other unfortunate wretch
                         //but you're not actually safe unless I lose my "clyde" variable

Share this post


Link to post
Share on other sites

clydesAge = 2;  //this is me crossing out what I wrote on the paper and writing "clydesAge is 2" -- it doesn't actually turn you into a baby!


This made me laugh out loud. 

Hopefully your explanation will leverage my narcissism in such a way that I will retain the information.

Share this post


Link to post
Share on other sites

There are actually two different levels of that thing not working.

 

The first is that if you have a reference and re-assign it you aren't changing the original object, you're simply re-assigning the reference. So "pos" was pointing at position but then it as made to point at something else.

 

The second is that Vector3 is not a reference type anyway, so in reality "pos" wasn't pointing at anything.

Share this post


Link to post
Share on other sites

I have a question about animating things like drawers/presses/doors etc.

 

I made a desk with 4 drawers in Blender and an animation for sliding them out. I exported it as fbx with a single drawer thinking I can duplicate that in Unity. Unity imports the object fine, but it when I drop the object into the scene it seems to come in as a group/folder. The icon in the inspector has a little folder on it and in the Hierarchy theres a desk prefab containing a sub desk and drawer. The imported animation seems to be attached to that rather than the drawer. I added an onMouseUp script to the drawer that calls this parent and runs the animation. This is working fine, click the drawer and it opens but It seems like this is the wrong way to do it because this animation only targets the drawer that was imported. So this means when I duplicate the drawer and click on it the imported drawer opens.

 

Should I be importing things like the drawer and their animation separately then positioning them in Unity or am I missing something stupid?

Share this post


Link to post
Share on other sites

Does anyone know of a way to have multiple people simultaneously editing a scene over the internet?

 

I've started on a project with 5 people and have been using git, which is great for all our scripts and assets, but having multiple people work on the same scene has been pretty difficult and a frustrating bottleneck at times.

 

I tried (somewhat feebly) finding a solution online and wasn't successful.

 

I'm currently frustrated with the lack of a "Save Project As" button in Unity. As I'm looking into it, I'm discovering that there is something called "Version control". Unfortunately, I wouldn't be finding out it exists unless I needed it... right now. Anyway, I found this thread that may be helpful to you.

http://answers.unity3d.com/questions/8740/version-control-workflow.html

 

Edit:

 Also found this:

http://docs.unity3d.com/Documentation/Manual/ExternalVersionControlSystemSupport.html

Share this post


Link to post
Share on other sites

I haven't figured out any clever way to do version control with Unity Free yet; but whenever I finish doing something time-consuming or difficult, I (try to =p) remember to copy my Scripts folder (or entire project, if it involved lots of scene editing) to Dropbox or Google Drive or someplace.

 

Hmm, writing this makes me realize I haven't done anything of the kind in far too long. =P

Share this post


Link to post
Share on other sites

Does anybody know why adding an NGUI object to a scene would screw up the existing camera?

I have a third person camera like so:

Q4v3Z7w.png

But as soon as I add NGUI to the mix, the player character stops being drawn by the existing camera and shows up on a different layer and only from the view of the NGUI camera.

nHS4HAW.png

The change is permanent and persists even if I delete the NGUI hierarchy (at which point the player character becomes invisible as the NGUI camera isn't there anymore). As far as I can tell, the only way to recover the ability to see the player character properly is to load the scene from before NGUI was added.

Edit: Apparently it's happening to the player character because it's on the "Player" layer. I remain baffled as to why NGUI would prevent you from seeing things on the "Player" layer, or why it continues to happen afterwards.

Share this post


Link to post
Share on other sites

I've been using git for version control with my Unity projects. You can set up a private git repository on bitbucket for free, and then create a .gitignore file in your project's root folder like this one. I then use the free program SourceTree to do the pushing (uploading) and pulling (downloading) of your unity project to and from the repository.

 

If you haven't worked with version control before it takes a bit of getting used to, and you might need to read up on the core concepts of how to use git, but I'd highly recommend getting version control going for any project.

Share this post


Link to post
Share on other sites

Lork - if this helps, on projects that I've worked on that've used NGUI, the main camera (not the NGUI one) had a UICamera script attached that had its Event Type set to World. In the Event Mask component of both the UICamera that's attached to your world camera, and the UICamera that's attached to your NGUI camera, you can toggle on and off which layers each camera renders.

Share this post


Link to post
Share on other sites

Has anyone used the relatively new Animation Events in the Mecanim animation system? (Not the OLD Animation Events in the eventually-to-be-deprecated old animation system.) They apparently went in several months ago but have not yet shown up in Unity's documentation. It's easy enough to call a function from an animation event at a given keyframe by adding it in the import settings for a given animation clip, but the event only seems to fire once per contiguous looping of that animation. What I mean is, if you assign an animation event to a keyframe of a walking animation, and you hold down W to walk in the game, the event will only fire once even if the animation loops several times. Then if you stop walking and start again, the animation event will fire again, but still only once per total "walk." I can't figure out why this would be the case. The event should fire every single time the animation hits that keyframe, but it doesn't. Anyone run into this?

Share this post


Link to post
Share on other sites

Chris I am not that familiar with the new Mecanim Events, but I just tested it for looping anims and it works for me.

In the import settings for my walk cycle I made an event then in the "Function" field I wrote "OnTestEvent", then in a script attached to the object that has an animator component that will call that animation I made a function OnTestEvent() that just prints some text to the debug console every time it's called. Works as expected.
Maybe try doing it with something on a new object & anim that isn't connected to anything else in the game and see if it works, then scale up from there to find where it stops working?
 

Share this post


Link to post
Share on other sites

This probably isn't it, but it's still worth mentioning so that people working with Unity are aware of it.

There's a toggle on the Console window labeled 'Collapse' - if you have it enabled, identical Debug prints that you're sending to the console will not be displayed twice - they'll collapse under one entry with a number on the left to indicate how many times that text has been sent to the console. I believe that option defaults to enabled, so in situations similar to Chris's, I've been tripped up by it, thinking that a piece of code is only firing once when it's actually going off multiple times.

Share this post


Link to post
Share on other sites

Anthony:

Yeah, I'll give that a try.

Jason:

That's not the issue because I'm also firing an audio clip and it has the same behavior. Although sometimes it works right! And sometimes it doesn't.

Share this post


Link to post
Share on other sites

We haven't really adopted Mechanim fully, using legacy animation events and state machines to trigger our stuff.  

 

This post I hope is true, but it's light in details and I haven't tried it out, so I can't back it up:

http://www.reddit.com/r/Unity3D/comments/1rgu6c/quick_43_mecanim_event_tutorial/

 

There are some brutal hacks being posted.  Such as measuring time or fixedupdate ticks then manually triggering your event (which is awful).  Or animating another value and monitoring it's state in another component (spiking the curve at a time you would otherwise put a keyframe event).

 

There's also a $35 asset store plug-in to add legacy style events to Mechanim.  I feel like that's a bit short sighted, but if it gets the job done, it might be worth using: http://forum.unity3d.com/threads/163731-Event-system-for-Mecanim

 

Sorry I can't be more help, but basically it looks like it's a bit broken.

Share this post


Link to post
Share on other sites

Then if you stop walking and start again, the animation event will fire again, but still only once per total "walk." I can't figure out why this would be the case. The event should fire every single time the animation hits that keyframe, but it doesn't. Anyone run into this?

 

This is mostly a guess, as I haven't worked with Mecanim all that much, but from what I understand this problem is a result of the way Legacy and Mecanim animation works.  With legacy animation you play the animations directly, where as with mecanim you transition between states that then control the playing of the animation.  I would imagine the reason you're only seeing the event fire once is because control of the animation is handled by mecanim, which likely only "plays" the animation once and then manipulates it as necessary.

 

If this is in fact correct, you should be able to get around it by listening for changes in the mecanim animation's state as opposed to inserting events directly into the animation.  A simple test could be to take the animation you are playing, then manually play it forward and reverse (not using ping-pong or looping, just manipulate the animation state's speed variable) and see if the event fires each time.

Share this post


Link to post
Share on other sites

I'm trying to make my own OnMouseDown type of method. As I'm doing it, I have a hard time understanding how someone made it so that every script fires off its OnMouseDown function without doing a GetComponent to a specific script. that is confusing. Let me use my example.

Here is the script that shoots a ray to determine what object I'm clicking on.

using UnityEngine;
using System.Collections;

public class MouseClick : MonoBehaviour {

	RaycastHit hit;
	
	void Update () {
		if (Input.GetMouseButtonDown(0)){
			if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit)) {
				Transform target = hit.transform;
				Command command =target.GetComponent<Command>();
				if(command!=null){
					Command.Click()=true;
				}
				
			}
		}
		
	}
}

Now I want every script to have the capability of calling its own Click() method. The Click() method may be different on each script. Does that make sense? If I include the GetComponent like I did in the script above, then I would have to put the exact same script ("Command") on different objects and write a bunch of conditionals to determine what to do when Click() happens. How does OnMouseDown() do it?

Edit, a few hours later:

While looking for alternatives to SendMessage, I kept coming across the phrase "over optimizing" so I'm just going to go with SendMessage for the time being. I feel like I learned some stuff on my search though, not sure what.

Share this post


Link to post
Share on other sites

The key thing you're missing is events. Instead of working with the individual components, you simply send an event to the gameobject, and any scripts attached to it will respond accordingly if you've given them the relevant receivers. That's essentially how OnMouseDown works, as well.

Share this post


Link to post
Share on other sites

I agree with Lork's statement about events, however, I went about it slightly different for my game.  When designing my game, I wanted the ability to be able to play the game on basically any platform (PC/Mac/iOS/Android/etc).  So this meant that sometimes I needed to catch a touch event, or a click event or even a joystick position, or button.  What I did is move all of that logic out into its own singleton class called "InputManager".  This class is instantiated and ensures that there is only ever one instance in the scene.  In this class I have the boiled down properties that I want to be able to check on such as "Fire", "ForwardThrust", "Special", etc. Basically any verb that I would ever need would be exposed in this class.  From there this class would use Unity's preprocessor directives to handle the different platforms and then map the different types of inputs to the appropriate variables.  Now that this is completed any script that needs any type of input just checks this class for the value of the input and the acts accordingly.

 

The huge benefit here is that in the instance of a game which has an aircraft flying, you can build to iPhone/Android and use onscreen touch joystick or build to PC/Mac and use a 360 controller without having to make any changes to code that handles flying the aircraft.

 

I know this doesn't directly answer your question about handling events, but if you were to combine what Lork mentioned with this, you get a lot of extensibility with your inputs.  I hope this helps somewhat.

Share this post


Link to post
Share on other sites

Y'all are an enormous help. I'm in a strange spot where I'm starting to understand why I would want to use these more advanced concepts without having a firm enough grasp on the basics to confidently start cramming on the intermediate stuff. Knowing what the terms are in order to research them is helpful, and I wouldn't be able to learn it at all without a use-case that I'm currently interested in. I think I need to spend some time brute-forcing the intermediate section of the Unity learning modules, up to this point, they've all seemed not only complex, but completely unapplicable. I spent the morning reading about generics, delegates, and events. It's not sinking in, but that's ok. I'll eventually ascend if I keep exposing myself to it. Singletons and coroutines are included in the intermediate section too.

Share this post


Link to post
Share on other sites

I had a bit of trouble when I first encountered events and delegates as well, just keep at it and it will make sense once you've had some time.  If you are interested in learning C# I can't recommend the XNA 101 series at 3D Buzz enough.  They go over everything you need to know about C#, down to how mono framework actually works, and even start off by writing a text adventure that plays in command line.

What helped me when learning about delegates and events was to think of them like to-do lists.  So instead of making a call to a function, you run through all the items in a to-do list.  They become particularly useful when you need to start having things respond to other things that are not currently on the same gameobject, such as a door opening or a unit dies.  Personally I use events extensively in my code over SendMessage.  The reason for this is that SendMessage won't work on disabled components or objects, and you can run into some weird situations because of it.  I'm not sure if this will make sense to you, but I think the best thing is to just see an example of them in action which I've attached to this post.  In that particular class you'll see examples of Attributes, events, delegates, and generics.

 

This is a monobehaviour and helper class I'm using for the game I'm currently working on that controls when actors take damage and raises the necessary events.  There will be errors since we're using a few plugins, but the basic functionality is all there.  Hope that helps.

 

Health Component Example.txt

Share this post


Link to post
Share on other sites

I want to save and load a grid of gameObjects including the properties of their component class (call this class "Difingo"). If I make the component class serializable, create a serializable class whose sole property is an array of instantiated Difingos, and then delete the original Difingos, will the serialized array class be able to recall all the properties of each Difingo instance? Or is it just an array of addresses whose residents have been destroyed?

Share this post


Link to post
Share on other sites

I want to save and load a grid of gameObjects including the properties of their component class (call this class "Difingo"). If I make the component class serializable, create a serializable class whose sole property is an array of instantiated Difingos, and then delete the original Difingos, will the serialized array class be able to recall all the properties of each Difingo instance? Or is it just an array of addresses whose residents have been destroyed?

 

What is this for?  If you are interested in scene serialization as a way of creating save games, there is actually a package on the asset store that will serialize the entire scene in a line or two of code.  So long as your saving mechanism writes all the properties you want for the Difingo class, those properties can be recalled.

Share this post


Link to post
Share on other sites

I'm using it to make levels through the Unity editor that I can load individually when someone is playing. I have a grid that is a step-sequencer, Once I make a song, I want to be able to save it to a file as the game-maker then load that file to instantiate the song for the player (Rather than build the scenes). Now that I'm think about it a bit more, I'm wondering if this would even save memory. The Unity editor is probably doing some form of serialization itself. Maybe I should just build scenes like normal people.

Actually, after spending a large amount of yesterday learning about serialization and trying to implement it, I came to the conclusion that I'd rather save the game state as a text document. It's kind of weird how I start looking into particular solutions and the realizing ten hours later that I'm deep in a rabbit-hole and I don't know why.

TLDR: I got confused somewhere along the process and didn't realize that this is exactly what scenes are for. No regrets, I learned a lot yesterday.

I'll probably end up using that asset for saving games. Thanks for the heads up.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now