Infinte runner ground loading on trigger not working
Hi guys, I know this is a topic that has probably been done to death but I have not been able to find a solution as yet in all the answers.unity world and google combined. Ok so here's the run down, I am doing a Diploma in interactive Gaming and one of my last units/modules is to make a game for mobiles, I have chosen to do an infinite runner based on a popular kids bedtime story (I have 4 kids) I have been following along with unity's infinite 2D runner as well as many others from coffeebreak and ninjacupcake83 (I have been using his the most as it closely resembles what I wish to achieve). I have Instantiated a prefab clone of my "floor" to begin with so the player can stand upon something, I have also added a collider trigger towards the end of the "floor" which is meant to instanitate the next bit of "floor" and then delete the prefab behind me. the problem is a warning that says this
"Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object) GroundSpawn:OnTriggerExit(Collider) (at Assets/Scripts/GroundSpawn.cs:29)"
My code is as follows
using UnityEngine;
using System.Collections;
public class GroundSpawn : MonoBehaviour
{
// Call prefab to assign
public GameObject worldClone;
// Offset for spawning ground
public float groundOffset = 0
;
// Prefabs position (x, y, z)
public Vector3 groundSpawn = new Vector3 (0, 0, 0);
// Box Collider set up with a enter trigger function
void OnTriggerEnter (Collider other)
{
// Spawn ground on the z axis where collider centers
groundSpawn = new Vector3 (0, 0, transform.position.z + groundOffset);
Instantiate (worldClone, groundSpawn, Quaternion.identity);
}
// Box Collider set up with a exit trigger function
void OnTriggerExit(Collider other)
{
// Removes prefabs on exit of Collider
Destroy (worldClone);
}
}
Answer by vintar · Jan 03, 2016 at 10:08 PM
You are trying to destroy the asset you dragged onto the inspector. Use this modified Script.
using UnityEngine;
using System.Collections;
public class GroundSpawn : MonoBehaviour
{
// Call prefab to assign
public GameObject worldClone;
GameObject _currentGround;
// Offset for spawning ground
public float groundOffset = 0 ;
// Prefabs position (x, y, z)
public Vector3 groundSpawn = new Vector3 (0, 0, 0);
// Box Collider set up with a enter trigger function
void OnTriggerEnter (Collider other)
{
// Spawn ground on the z axis where collider centers
groundSpawn = new Vector3 (0, 0, transform.position.z + groundOffset);
_currentGround = Instantiate (worldClone, groundSpawn, Quaternion.identity) as GameObject;
}
// Box Collider set up with a exit trigger function
void OnTriggerExit(Collider other)
{
// Removes prefabs on exit of Collider
Destroy (_currentGround);
}
}
Thank you I see were I was going wrong now. $$anonymous$$udos to you for pointing it out to me
Can I use a similar script to destroy it using a different trigger collider ? I am trying something different where I am moving the ground towards the player but the instantiated floor does not trigger a new clone when it passes into the trigger collider, I have set a box collider and rigidbody to the "floor" piece, but to no avail, TIA