Prefabs and destroy

Hi, i’ve wrote some code that has the job to instantiate a cube and activate a little animation made through the lerp method, everything works fine except a couple of things:

-the editor says destroying prefabs is prohibited: i didn’t mean to do that and i don’t know how to resolve the issue (even if it’s not game breaking by any means, it suggests i haven’t understood fully how prefabs works)

-the moment the lerp script is activated is activated also on the prefab, so i have to make sure when i start the game it is deactivated and that is not the best solution.

Note: the prefab is a simple cube with two scripts attached: lerp and unlerp, that are simply bringing the cube over the game plane and bringing it back down, both are deactivated initially.

Here is the code:

using UnityEngine;
using System.Collections;

public class RayCast : MonoBehaviour
{
    public GameObject cube;
    public GameObject oldCube;
   
    void Update()
    {
        if (Input.GetButtonDown ("Fire1"))
        {
            //create a ray perpendicular to the camera plane where the mouse clicks
            Ray vRay = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;

            // checks if there is collision between vRay and colliders
            Physics.Raycast(vRay.origin, vRay.direction,out hit, 9999);

            if(hit.transform.name.Equals("Floor"))
            {
                //cube creation
                float pX = Mathf.Floor(hit.point.x)+0.5f;
                float pZ = Mathf.Floor(hit.point.z)+0.5f;
                Vector3 position = new Vector3 (pX, -0.5f, pZ);

                oldCube = cube;
                cube = GameObject.Instantiate(cube, position, Quaternion.identity) as GameObject;

                if(cube.GetComponent<Unlerp>().enabled == true)
                    cube.GetComponent<Unlerp>().enabled = false;

                cube.name = "SpawnedCube";
                //this is here just to have the hierarchy a bit cleaner

                //activating Lerp Script
                cube.GetComponent<Lerp>().enabled = true;

                if(!(oldCube == null))
                {
                    //activating unLerp script on precedent cube
                    oldCube.GetComponent<Unlerp>().enabled = true;
                }

                //destroying the old cube
                Destroy (oldCube.gameObject, 1.5f);
            }
        }
    }
}

The error message appear when trying to destroy an object in the assets folder instead of an instance of the Prefab in-game. Atleast it was when I bumped into it myself awhile ago.

I suppose you could solve it by either having cube and oldCube only ever contain the Prefab references and then using two private fields for the in-game instances of the two.

public GameObject cubeTemplate;

private GameObject _curCube;
private GameObject _oldCube;

public void YourMethod(){
//At the destroy thingie of yours
if(_oldCube != null ){
Destroy( _oldCube );
}

_oldCube = _curCube;
_curCube = (GameObject)Instantiate( cubeTemplate, position, Quaternion.identity );
}

Another fix could be to add a boolean to toggle if _oldCube has been assigned yet (Since the problem most likely occurs at the first time the code runs through as oldCube has not yet been assigned to an instance but still Points at the Prefab asset.) and after the first time you assign an instance to the oldCube field you toggle the bool to true.

:slight_smile: Hope it helps!

Kona, thanks for sharring this code, it works great with me, but is there a way for me to specify how many prefabs stay alive ? because right now I have only 2 instances of my prefab and the other ones are destroyed.

thank