triggering appearance/disappearance of animated object by keyboard

Heya folks :),

I’ve asked a question here before, and got really amazing feedback. I will certainly add the unity community to my acknowledgments :slight_smile: when my article gets pulbished :). Before I start, I have to say I am not a professional, not even a hobbyist modeller/gamebuilder. I am a hobbyist nerd and fervent gamer, but the world of 3D modelling and game coding was alien to me before I started this internship. I am a masterstudent neuroscience and cognition, fiddling around with all of this in the name of science. So if you feel exasperated by the simplicity of my questions, or feel like I should have searched myself, please note that I did, but my knowledge is probably insufficient to determine the right search terms. Please phrase your answers so that they can be understood by a complete noob.

For my current experiment, I’m using a 3D representation of my real lab, modeled in 3DSmax, and exported to unity. In this lab, a virtual person sits at a table, and the view from this virtual persons eyes is presented to my participant using stereoscopic glasses (e-magine Z800). In each trial, a visual cue needs to be presented for about one second, then a during 250 or 500 ms nothing happens, then a tactile stimulus is delivered by devices taped to the hands, and then the participant needs to press a button in response. In my pilots the visual cue was a lightflash of 300 ms. So far so good.

In my main experiment however, I want to use animations of animals. I’ve found some animated and textured models on turbosquid and I’ve been messing around with them a bit, but I’ve not been able to separate the animations. The object just runs through all animations in order, and then stops.

Q1) Is there a possibility to loop one of these animations continuously?

In addition, I want the animals to appear and disappear at precise moments in time. The timing must be perfect, a variable variance above 10 ms will kill the effect I am measuring. To be able to control timing like this I’ve been using emulated keyboard input in MatLab, which the entire experiment is coded in. Others have suggested writing a plugin, but unfortunately, I don’t have the time or experience to do such a thing within this internship, I need to stick to my current setup. So,

Q2) Is there any way to program unity to let an object appear, and then disappear when a certain key is pressed? Just like I have the program to turn the lights on and off?

Those of you who’ve taken the time to read the entire post, thank you very much. Answering my questions contributes to science, so please do so ;).

Q1) Enter “unity split animation” into google. Click on first link:

http://unity3d.com/support/documentation/Manual/Character-Animation.html

Page appears about character animation in Unity. Scroll down to the section that is titled “Importing Animations using Animation Splitting”. Read about how you can enter start and end frames to break a long sequence of animation into separate clips. Any of these clips can then be looped.

Q2) Create a script and add the following code:

var go : GameObject;

function Update () {
	if (Input.GetKeyDown ("space")) {
		Debug.Log ("Space key pressed");
		
		go.active = (go.active) ? false : true;
	}
}

Add this script to your camera. In the inspector set the go variable to the game object you want to control.enter code here

Q1) Is there a possibility to loop one of these animations continuously?

If you are referring to a Unity Animation Clip then yes. You can select it and in the inspector set the WrapMode to Loop.

Q2) Is there any way to program unity to let an object appear, and then disappear when a certain key is pressed? Just like I have the program to turn the lights on and off?

You can try:

void Update() {
if (Input.GetKeyUp(KeyCode.Space))
{
GetComponent().enabled = !GetComponent().enabled;
}
}

Ok, I’ve been fiddling around for a few days, and I’m drowning in errors. The splitting and looping of the animations works, but the appearing and dissapearing doesn’t. @Graham Dunnett, I can add the script and appoint it to an object (most of the time, sometimes unity does a CTD when I try to appoint) but adding the script to the camera does not make the space work as a trigger. The log shows space has been pressed, the animal just stays there. I get the code that this doesn’t work for a prefab object? @Ricardo Arango, when trying your code, unity tells me it expects a ;, and that I should add one at the end of the script. When I do that and try to run, unity does a CTD. Edit: OK, found out I ran the wrong kind of script for yours, but even if I add it into a C sharp script, it keeps telling me that it’s not done compiling whenever I try to do anything with it. I tried to fit it into the standard script it gives me when I create the new script file, with the same results.

Edit @Graham Dunnett OK, now every time I try to appoint the GO Unity CTDs… Even after reloading all the assets in a new project. Nice.

Also, I always get the warning that “the prefab connection will be undone” when I try to add any script to the animal models.

Any other ideas I could try? Could my installation of Unity be bugged in some way?

Edit: After adding 1 fbx model into a completely clean project I get this error: NullReferenceException: Object reference not set to an instance of an object
UnityEditor.ObjectSelector.OnDisable () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/ObjectSelector.cs:138)