dont know where my problem

HI
I wrote this code along time ago:

public class Gun : MonoBehaviour 
{
	public Rigidbody Bullet;
	public int speed = 100;
	public Transform spawn;
	
	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Mouse0)==true)
			Spawn();
	}
	
	void Spawn()
	{
	 Instantiate(Bullet,spawn.position,spawn.rotation);
	 Bullet.AddForce(Vector3.forward*speed);
	}
}

and I dont know where my problem is
when I click on the mouse it create just the bullet but the bullet dosn’t move
where is my problem?

thx for all the helpers

Just mentioned this to someone else, your applying the force to your prefab, not the GameObject your creating.

GameObject g = Instantiate(Bullet,spawn.position,spawn.rotation);
g.AddForce(Vector3.forward*speed);

Also next time, you should use CODE tags not the PHP ones :slight_smile:

its not work’it do me a problem:
Assets/Gun.cs(18,21): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)

My bad, i forgot Unity doesnt return a GameObject

GameObject g = (GameObject)Instantiate(Bullet,spawn.position,spawn.rotation);

There easy as that

Theres steel 1 problem:
Assets/Gun.cs(18,21): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)

Well there shouldnt be a problem with that piece of code

If Bullet is a Rigidbody (which it is in your code) then you shouldn’t try to cast it to GameObject when you Instantiate it.

GameObject g = (GameObject)Instantiate(Bullet,spawn.position,spawn.rotation) as GameObject;