- Home /
how can i use an array to do multiple animations? c#
Hey there,
I want to make a system where my platform my player stands on changes with the animation I've set it up with. I created an array but i get an error, i must confess im still learning arrays as they don't agree with me. Anyways can anyone suggest what i can do? here is the current code:
using UnityEngine;
using System.Collections;
public class CameraSwitch : MonoBehaviour {
public GameObject Player;
private GameObject[] Platform;
public GameObject Playerobj;
public AudioClip CameraSWtich;
void Start(){
Platform = GameObject.FindGameObjectsWithTag("Platform");
}
void OnGUI () {
if(PlayerPrefs.GetInt("ActionBar",0) == 1){
if(GUI.Button(new Rect(Screen.width /2 -150,Screen.height/2 + 200,50,50),"3D")){
audio.clip = CameraSWtich;
audio.Play();
animation.Play("3dCamera");
Playerobj.animation.Play("playerout");
Platform.animation.Play("brick3d");
TP_Controller tpc = Player.GetComponent<TP_Controller>();
tpc.thirdDOn = true;
}
}
}
}
What part isn't working? Its hard to tell just by looking at your script. What is the error you receive?
sorry i forgot to add the error, this is the error : Type UnityEngine.GameObject[]' does not contain a definition for
animation' and no extension method animation' of type
UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)
Answer by SubatomicHero · May 15, 2013 at 11:21 AM
I think this could possibly solve your problem:
// change this at the top
private GameObject[] Platform;
// to this:
private Animation[] Platform;
// In start change the code to this:
void Start() {
Platform = GameObject.FindGameObjectsWithTag("Platform") as Animation;
}
Then update the code in your OnGUI function. WARNING this is untested code
sorry it still doesn't work, i get this error : Type UnityEngine.Animation[]' does not contain a definition for
animation' and no extension method animation' of type
UnityEngine.Animation[]' could be found (are you missing a using directive or an assembly reference?)
It's odd tbf, I can't actually think of a way of doing it since it's multiple objects prefor$$anonymous$$g the same animation at once. :/
did you try changing:
Platform.animation.Play("Whatever");
// to this
Debug.Log(Platform.toString());
So you can see what your array contains?
Yes, I get these two errors : Cannot convert type UnityEngine.GameObject[]' to
UnityEngine.Animation' via a built-in conversion
And : Type UnityEngine.Animation[]' does not contain a definition for
toString' and no extension method toString' of type
UnityEngine.Animation[]' could be found (are you missing a using directive or an assembly reference?)
ah then in start it should be
Platform = FindObjectsofType(typeof(Animation)) as Animation[];
Also don't forget you need to access an element of your array, which I forgot to mention in my answer.
// access the first element(animation) in your array
Platform[0].animation.Play();
// or you could use a for loop
for (int i = 0; i < Platform.Length; i++)
{
Platform[i].animation.Play();
}
But from now I think you need to play with your code, I've given enough ideas to help :D
just tried that, have the exact same errors as last time. this doesn't want to work for us haha
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using a Parameterized arraylist (C#)??? 1 Answer
Best way to keep track of objects on a 3D Grid? 2 Answers
How to debug values in jagged arrays? 0 Answers