I ask myself what I did wrong. The second animations by pressing ‘e’ work but the others do not. The others do not start at all.
This script is for the FPS controller. All objects are filled in.
For example the ‘BuggyDoorLeft1’ GameObject has its animation attached, which I want to get via code.
The objects are from one model which I have created with Cinema 4D. The fbx structure is hierarchic like.
Object
_Object1
__BuggyDoorLeft1
___ Mesh …
___Mesh …
I marked the animations as ‘Legacy’ too. The objects got the ‘animation’ component with an array and the attached animation. I received no error.
public GameObject BuggyDoorLeft1;
public GameObject BuggyDoorRight1;
private bool BuggyDoorReverse = false;
public GameObject BuggyStarter;
private bool BuggyStartReverse = false;
public GameObject BuggyItself;
public GameObject GrandDoorUp;
public GameObject GrandDoorDown;
private bool GrandDoorReverse = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(transform.position, BuggyStarter.transform.position) <= 200 )
{
if (Input.GetKey(KeyCode.T))
{
Debug.Log("condition fulfilled");
if (BuggyDoorReverse == true)
{
var b = BuggyDoorLeft1.GetComponent<Animation>();
b["DoorLeft"].speed = -1;
b.Play("DoorLeft");
var d = BuggyDoorRight1.GetComponent<Animation>();
d["DoorRight"].speed = -1;
d.Play("DoorRight");
BuggyDoorReverse = !BuggyDoorReverse;
}
if (GrandDoorReverse == false)
{
var c = GrandDoorUp.GetComponent<Animation>();
c["DoorUp"].speed = (GrandDoorReverse == false ? 1f : -1f);
c.Play("DoorUp");
var e = GrandDoorDown.GetComponent<Animation>();
e["DoorDown"].speed = (GrandDoorReverse == false ? 1f : -1f);
e.Play("DoorDown");
GrandDoorReverse = !GrandDoorReverse;
}
var a = BuggyItself.GetComponent<Animation>();
a["BuggyDrive"].speed = (BuggyStartReverse == false ? 1f : -1f);
a.Play("BuggyDrive");
var y = BuggyStarter.GetComponent<Animation>();
y["SwitchDrive"].speed = (BuggyStartReverse == false ? 1f : -1f);
y.Play("SwitchDrive");
BuggyStartReverse = !BuggyStartReverse;
}
}
if (Vector3.Distance(transform.position, BuggyDoorLeft1.transform.position) <= 200 || Vector3.Distance(transform.position, BuggyDoorRight1.transform.position) <= 200)
{
if (Input.GetKey(KeyCode.E))
{
var a = BuggyDoorLeft1.GetComponent<Animation>();
a["DoorLeft"].speed = (BuggyDoorReverse == false ? 1f : -1f);
a.Play("DoorLeft");
var d = BuggyDoorRight1.GetComponent<Animation>();
d["DoorRight"].speed = (BuggyDoorReverse == false ? 1f : -1f);
d.Play("DoorRight");
BuggyDoorReverse = !BuggyDoorReverse;
}
}