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);
}
}
}
}