Funkcja Instantiate nie działa w unity

0

Jestem początkującym programistą. Tworzę w unity grę w której latasz statkiem kosmicznym po dwuwymiarowej płaszczyźnie i strzelasz do kosmitów ale w chwili użycia funkcji Instantiate w celu utworzenia pocisku, program się zatrzymuje i pokazuje błąd:

InvalidCastException: Specified cast is not valid.
(wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <f1212ad1dec44ce7b7147976b91869c3>:0)
sterowanie.Update () (at Assets/scripts/sterowanie.cs:38)

pełny kod:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using UnityEngine;

public class sterowanie : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    [SerializeField] GameObject player;
    [SerializeField] Rigidbody bullet;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.A))
        {
            if(player.transform.position.z < 6.5f)
            {
                player.transform.position += new Vector3(0f, 0f, 0.01f);
            }
        }
        if (Input.GetKey(KeyCode.D))
        {
            if (player.transform.position.z > -6.5f)
            {
                player.transform.position -= new Vector3(0f, 0f, 0.01f);
            }
        }
        if(Input.GetKey(KeyCode.Space))
        {
            Rigidbody clone = Instantiate(bullet, player.transform.position, player.transform.rotation) as Rigidbody;
            Vector3 fwd = transform.TransformDirection(Vector3.forward) ;
            clone.AddForce(fwd);
        }
    }
}

Proszę o szybką pomoc.

1

"InvalidCastException" oznacza, że próbujesz coś źle 'konwertować', z typu na typ,
sam kod wygląda chyba okej,
a jak dasz bez końcówki linijki "as rigidbody" to nie jest okej?

1 użytkowników online, w tym zalogowanych: 0, gości: 1