Problem with Rigibody

I am trying to add a force to a bullet casing but i get an error the line "public Rigidbody rb = bulletCase.GetComponent(); " has a red line below bulletCase.

the error is “A field intitializer cannot reference the non-static field or method”

using UnityEngine;
using System.Collections;

public class spawnHuls : MonoBehaviour
{
    public float trowSpeed;
    public GameObject bulletCase;
    public Rigidbody rb = bulletCase.GetComponent<Rigidbody>();
    void start()
    {
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(bulletCase, transform.position, transform.rotation);
            rb.AddForce(0, 0, trowSpeed);
        }
   
    }
}

You can’t use GetComponent outside of a method.
Since the rigidbody field is public, you can assign it through the inspector.

Or, you can assign the variable in the Start method that you have, or in the void Awake() method.

How do i assign the rigibody in the inspector?

You simply click and drag the rigidbody component, into the spawnHuls’ rigidbody field in the inspector, like so:
2654541--187099--AssigningInInspector.gif

i have the rigibody component in the prefab of the bullet and i have the script in an emty gameobject for the spawn location of the gameobject, so i need to change the rigibody from the prefab to the emty game object?

Ah, okay.
This will be a little different then.

What you want to do, is store the created bullet casing when you instantiate it.
Then, you can grab its rigidbody, and apply the needed force.

So, change your script like so:

using UnityEngine;
using System.Collections;
public class spawnHuls : MonoBehaviour
{
    public float trowSpeed;
    public GameObject bulletCase;
    public Rigidbody rb ;

    private GameObject createdCase;

    void start()
    {
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
          // Create a new casing, and store it for latter use.
          createdCase =  Instantiate(bulletCase, transform.position, transform.rotation);
          // Get the casing's rigidbody.          
          rb = createdCase.GetComponent<Rigidbody>();
          rb.AddForce(0, 0, trowSpeed);
        }
 
    }
}

error:

Severity Code Description Project File Line Suppression State
Error CS0266 Cannot implicitly convert type ‘UnityEngine.Object’ to ‘UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?) game.CSharp C:\Users\MyNameIsSecret\Dropbox\game\Assets\scrips\weapon\spawnBulletCase.cs 19 Active

Try adding (GameObject) right after the equals sign where you are likely getting this error.
Also, giving us the line of code that is causing this error would help us help you more. :slight_smile:

when i rotate the player the bullet casing falls one way only, it doent rotate with the player and the spawn casing gameobject is a child of the player.

Double click on the error in the Unity Console, it will take you to the line of code giving you this error.

EDIT: Ah, I think I see the problem.
Add (GameObject) between the equal sign, and the Instantiate call on line 19.
So that you have:

createdCase =  (GameObject)Instantiate(bulletCase, transform.position, transform.rotation);

well it still doent work but ive made some pictures and if you see some weard words its probably dutch.
(that createCase = GameObject.Find(“spawnHuls”); is a test so ignore it )

You might try AddRelativeForce instead of AddForce.

yessss, that worked! thank you verry mutch!