-2
\$\begingroup\$

I need some help making this script work.

#pragma strict

function OnMouseDown () {
Destroy (gameObject);
}

Basicly what the script does is that it destroys the objects that the player clicks on.I also got another object which has a script attached to it,which makes it spawn other objects.

The script clones the objects and makes it spawn multiple times.I attached the first script to the prefab but when I click on it,it won't be destroyed.If I click on the original prefab it works,but not on the clones created by the other script.

Anyone has any idea ?

Thank you.

Edit #1

Spawn script :

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {

    public GameObject[] obj;
    public float spawnMin = 1f;
    public float spawnMax = 2f;

    void Start () {
        Spawn ();
    }

    void Spawn() 
    {
        Instantiate(obj[Random.Range(0, obj.GetLength(0))],transform.position, Quaternion.identity);
        Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
    }

}
\$\endgroup\$
9
  • 1
    \$\begingroup\$ I attached the first script to the prefab Did you attach the script before or after instantiating clones? The clone objects need to have the script attached. \$\endgroup\$
    – jhocking
    Jun 13 '14 at 18:04
  • \$\begingroup\$ I actualy DON'T KNOW how instantiation works...Im new in scripting. \$\endgroup\$
    – Ssiro
    Jun 13 '14 at 18:25
  • \$\begingroup\$ If you don't know how instantiation works, perhaps you should ask about that instead. \$\endgroup\$
    – jhocking
    Jun 14 '14 at 1:55
  • 1
    \$\begingroup\$ Since you don't seem responsive to my previous suggestion to ask about instantiation in your question, let me try a different tack: in your question you said the first script makes it spawn multiple times. If you don't know how instantiation works, what did you mean by "spawn"? \$\endgroup\$
    – jhocking
    Jun 15 '14 at 5:32
  • \$\begingroup\$ By spawn I mean clone a prefab and put it on the scene multiple times. \$\endgroup\$
    – Ssiro
    Jun 16 '14 at 19:22
0
\$\begingroup\$

The code above is indeed correct, you MUST instantiate your objects though!
I understand how one cannot have knowledge on this topic, we all start somewhere.

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour
{
    public float Delay = 4f;       // Time before spawning starts, to allow scene to render/catch up
    public float Difference = 7f;  // Time between each spawn
    public GameObject[] enemies;   // Array of your enemy prefabs.


    void Start ()
    {
        InvokeRepeating("SpawnEnemy", Delay, Difference);
    }


    void SpawnEnemy ()
    {
        // Instantiate one random enemy every time this is called
        int enemyIndex = Random.Range(0, enemies.Length);
        Instantiate(enemies[enemyIndex], transform.position, transform.rotation);

    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .