I used some particles from the unity asset store for a cooler effect.
I followed every step:
- open my player prefab
- dragged the prefab particle from my assets onto the player prefab
- assigned the particle from the prefab hierarchy (and not from the assets) onto the playerController script
- unchecked the “play on awake” both from the particle inside the prefab, and also from the player in the main hierarchy
But yet, when I play my game, and when I call the particles.Play() upon tapping space, I get the:
the object of type particlesystem has been destroyed
so obviously nothing work
here is my code, very simple
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] float moveSpeed = 2f;
[SerializeField] ParticleSystem jumpEffect;
void Start()
{
Debug.Log("Hello world");
}
// Update is called once per frame
void Update()
{
MovePlayer();
}
void MovePlayer()
{
float x = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float z = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(x, 0, z);
if (Input.GetKeyDown("space"))
{
Debug.Log("space key was pressed");
if (jumpEffect != null)
{
jumpEffect.Play();
} else
{
Debug.Log("jump particle was destroyed");
}
}
}
}