my code problem

public class Playerweapon : MonoBehaviour {

    public GameObject bulletPrefab;

    public Transform bulletSpawn;

    public float bulletSpeed = 30;

    public float lifeTime = 3;


    #region Monobehaviour API

    void Start()
    {

    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Fire();
        }
    }

    private void Fire()
    {
        GameObject bullet = Instantiate(bulletPrefab);

        Physics.IgnoreCollision(bullet.GetComponent<Collider>()),
            bulletSpawn.parent.GetComponent<Collider>());

        bullet.transform.position = bulletSpawn.Position;

        Vector3 rotation = bullet.transform.rotation.eulerAngles;

        bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);

        bullet.GetComponant<Ridgedbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);

        StartCoroutine(DestroyBulletAfterTime(bullet, lifeTime));
    }

    private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
    {
        yield return new WaitForSeconds(delay);

        Destroy(bullet);
    }

    #endregion

}

Hey! You didn’t tell us what the problem is. But from a quick look over your script i can see you are getting the component “Ridgedbody” in line 41. I guess you meant “Rigidbody”?

Im new to unity and im trying to learn how to make a gun shoot but when i drag this script into anything to make it a component i get a error saying cant add script. the name player weapon is the same inside the script and unity. I have 2 errors CS1513 and CS1002, i know these mean i have a missing ; and a missing } but i cant find it please help

Im new to unity and im trying to learn how to make a gun shoot but when i drag this script into anything to make it a component i get a error saying cant add script. the name player weapon is the same inside the script and unity. I have 2 errors CS1513 and CS1002, i know these mean i have a missing ; and a missing } but i cant find it please help

Line 32 - Change this:

to this (check the brackets: ()):

Physics.IgnoreCollision**(bullet.GetComponent(), bulletSpawn.parent.GetComponent())**;

Basically it goes like this: Every function can have parameters which will go inside the () after the function name. A parameter can be a function itself, so it also comes with () after the name.

So in your case you have got the function IgnoreCollision**()** which takes two parameters. These two parameters are functions and therefore also have the (). Just make sure you are getting the encapsulation right.

1 Like

So does this mean i just need to get rid of the () so the code is just Physics.IgnoreCollision(buller.GetComponent<Collider>), bulletSpawn.parent.GetComponent<Collider>);

No, the () are needed!
To break it down:
You have the function IgnoreCollision()
This function needs to know which colliders to ignore. You are providing this information via the GetComponent() function (for each collider).

So in pseudecode you are doing this: IgnoreCollision(Collider1, Collider2)
As “GetComponent” is a function it also needs to have the () after the name (Tho it does not need any parameters in this case).

Physics.IgnoreCollision(bullet.GetComponent(), bulletSpawn.parent.GetComponent());

1 Like

OHHHHH i understand what you mean with the brackets thanks i just fixed it but i still have 3 other errors, they are CS1002 CS1003 and CS1038 and i dont know anything about them

This happens, if you have compile errors, after you fixed your code, igt should be possible to add the script.

There are still the () behind GetComponent() missing. @GoliathAT posted the correct line already.

Thanks man yeah i see what i did wrong but just out of curiosity why does it have brackets with nothing in after ()

Thats a legit question, as there should not be a need for the () if there are no parameters to fill it - you are right.
It IS needed however, as this is the way to tell the compiler that the thing is a function, and not just a variable.

1 Like

Ahhh okay thanks i understand this part now but i still “cant add script”

Please share the current script again.

i have also have 2 errors saying im missing 2 semi colons and 2 }

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playerweapon : MonoBehaviour
{

    public GameObject bulletPrefab;

    public Transform bulletSpawn;

    public float bulletSpeed = 30;

    public float lifeTime = 3;


    #region Monobehaviour API

    void Start()
    {

    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Fire();
        }
    }

    private void Fire()
    {
        GameObject bullet = Instantiate(bulletPrefab);

        Physics.IgnoreCollision(bullet.GetComponent<Collider>()),
            bulletSpawn.parent.GetComponent<Collider>());

        bullet.transform.position = bulletSpawn.Position;

        Vector3 rotation = bullet.transform.rotation.eulerAngles;

        bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);

        bullet.GetComponant<Ridgedbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);

        StartCoroutine(DestroyBulletAfterTime(bullet, lifeTime));
    }

    private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
    {
        yield return new WaitForSeconds(delay);

        Destroy(bullet);
    }

    #endregion

}

On line 37 is a “)” to much:

Physics.IgnoreCollision(bullet.GetComponent()),
bulletSpawn.parent.GetComponent());

remove that.

1 Like

You still have a “)” too much in there. Right after the first GetComponent() you are closing the IgnoreCollission with another “)”, so it thinks the function only takes 1 parameter.

Delete the second “)” after the first Get Component call. It is only needed after the second call, where we want to close the () for the whole IgnoreCollission function.

1 Like

okay yep thanks i have done that but now have these errors 6444739--721399--upload_2020-10-22_11-26-19.png

Wrong: Position
Correct: position

Wrong: Ridgedbody
Correct: Rigidbody

Wrong: GetComponant
Correct: GetCompon__e__nt

1 Like

dam i just messed up simple stuff my bad, however i still have one more error that is to do with another script
6444778--721408--upload_2020-10-22_11-39-34.png