x


new class to create object

The following code [targetdown.cs]attached to an object ,name as target.

it have two animation, up and down , if some colliders touch this target,it will play "down" animation. If three seconds time up, it will play "up" animation.

the another code[win.cs] also attached the target. If consecutive hit two target, it will be generated a battery.

Now I want to write these functions in a class , not monobehavior. If when I need, I new a class to generated a target.

How to write a class to generate target all function?

public class targetdown : MonoBehaviour {

    public GameObject targetRoot;
    private bool beenHit = false;
    private float timer = 0.0f;
    public AudioClip hitsound;
    public AudioClip resetSound;

    void OnCollisionEnter(Collision theObject)
    {
     if(beenHit==false && theObject.gameObject.name=="Copycoconut")
     {
            audio.PlayOneShot(hitsound);
         targetRoot.animation.Play("down");
         beenHit=true;
         CoCoWin.targets++;
     }
    }
    void Update()
    {
     if(beenHit)
     {
     timer+=Time.deltaTime;
     }
     if(timer>3)
     {
     audio.PlayOneShot(resetSound);
     targetRoot.animation.Play("up");
     beenHit = false;
     timer = 0.0f;
     CoCoWin.targets=0;
     }
    }

[win.cs]

public class win : MonoBehaviour {

    public static int targets = 0;
    private bool haveWon = false;
    public AudioClip winAudio ;
    public GameObject battery ;

    private GameObject launcher;
    void Update () 
    {
        if (launcher == null)
        {
            launcher = GameObject.Find("Launcher");
        }

        if(targets == 2 && haveWon == false)
     {
            launcher.GetComponent<ObjectThrow>().CanThrow = false;
            targets = 0;
     audio.PlayOneShot(winAudio);
     GameObject clone ;
     clone = Instantiate(battery,new Vector3(transform.position.x,transform.position.y,transform.position.z),
                                transform.rotation) as GameObject;
     clone.transform.localScale=new Vector3(100,100,100);
     haveWon=true;
     }
    }
more ▼

asked Jul 19 '12 at 10:03 AM

NMNW gravatar image

NMNW
1 2

I guess I'm a bit lost as to why you want a class rather than a MonoBehaviour derivative. You are using Update etc... Can you explain why you need the class?

Jul 19 '12 at 10:41 AM whydoidoit

mmm, I just want to practice using the class, or can give some sample for class code?

Jul 19 '12 at 02:56 PM NMNW
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x336

asked: Jul 19 '12 at 10:03 AM

Seen: 282 times

Last Updated: Jul 19 '12 at 02:56 PM