Hello all, I finding that one aspect of my game is fighting me every single step of the way and any help here would be appreciated. I have a bit understanding of C# but I’m not a guru by any stretch.
So here is the issue, I am following an online tutorial to instantiate bullet, or in my case a blast the shoots out from the player. So far so good on getting the blast to spawn from the middle of the player but then comes the challenge. The blast stays where I leave it or press the space bar - forevermore radiating out. This would be great for dropping mines or decoys but that isn’t my intent. The blast needs to happen, disappear, and reappear when space is pressed again etc.
I’m getting a warning saying that : “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, Single)
PlayerAtk:fire() (at Assets/Scripts2019/PlayerAtk.cs:35)
PlayerAtk:Update() (at Assets/Scripts2019/PlayerAtk.cs:24)”
I have also tried : Destroy(this, 2f); //it just kills the script
I have also tried : Destroy(EMP2, 2f); //says I can’t delete and asset
I have also tried : Destroy(gameObject, 2f); //says I can’t due to a null object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAtk : MonoBehaviour {
// Use this for initialization
public GameObject EMP2; //= player // Calls Emp Prefab
Vector2 blastPos; // variable for where to put he blastpos
public float fireRate = 0.5f;
float nextFire = 0.0f;
void Start (){
// perhaps find picks ups to enable limited number of blasts
}
void Update()
{
if(Input.GetKeyDown("space") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
fire ();
// when space is pressed, this will call blast wave - "void fire" and all that happens
}
}
void fire()
{
Debug.Log("blam!");
blastPos = transform.position;
blastPos += new Vector2(+0f, 0f);
Instantiate (EMP2, blastPos, Quaternion.identity);
Destroy (gameObject, 2f);
//https://www.youtube.com/watch?v=1QOsUrXWMWY
//7.30 blast creates, is solid, needs to vanish, rigid body,
// elements of what "Attack" does eg. sends rigid bodies flying,
// enemies flying in a radius around the player.
// enemies only
}
}
Thanks for your help, tips, and comments!