im having trouble with Identifier expected

pleas help i just cant figure it out

using UnityEngine;
public class Projectile : MonoBehaviour
{ public float damage = 1f;
public float IPForce = 30f;
private float speed = 0f;
public float bulletspeed = 4;

void OnCollisionEnter(blah)
{ blah.GetComponent();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * IPForce);
}
}
}

Hi and welcome.
Before i address your problem, please consider the following:

  • Use code tags to post code examples. Adds proper formatting and syntax highlighting. Makes it easier to help you. If you dont know how to use code tags, there is a sticky forum post on this subforum explaining how.
  • Always add the full error message if you have problems with an error. This includes the line the error is thrown, and should the example not include the full script, i would also expect you to tell us what line in the posted example this refers to.
  • A little description of the problem usually also helps.

With that out of the way, in line… whatever that is. See? Makes it quite hard to help :stuck_out_tongue:
Anyways, you write blah.GetComponent(). This returns you the component of type ‘target’ on the GameObject ‘blah’. You never save this anywhere tho. So what you probably intended to do is write 'Target target = blah.GetComponent();. On that note, class names like ‘target’ should start with a capital letter (look up some naming conventions).

So currently you write target.TakeDamage(), but the programm has no idea what ‘target’ is since you did never define it. Thus it tells you that it expects an identifier (the name of a variable) there. With the suggestion above, you would now actually have a variable with the identifier ‘target’, so this problem at least should be fixed.

using UnityEngine;
public class Projectile : MonoBehaviour
{
    public float damage = 1f;
    public float IPForce = 30f;
    private float speed = 0f;
    public float bulletspeed = 4;

    void OnCollisionEnter(Collider blah)
    {
        blah.GetComponent<target>();
        if (target != null)
        {
            target.TakeDamage(damage);
        }
        if (hit.rigidbody != null)
        {
            hit.rigidbody.AddForce(-hit.normal * IPForce);
        }
    }
}

You need to declare Collider, as OnCollisionEnter doesn’t know what blah is (assuming blah is a Collider)

void OnCollisionEnter(Collider blah)

not

void OnCollisionEnter(blah)
1 Like

Ok so, with what @davidnibi just posted, you actually need to fix these two problems for it to work.
When you write ‘void OnCollisionEnter’ your IDE should normally already offer you the autocompletion, since OnCollisionEnter is a function which is defined in MonoBehaviour and you just fill it with some content.

Since you seem to be very new to programming, i can generally recommend this tutorial series on C# & Unity:
Tutorials Playlist

okei thanks guys i got it what that meant to do is my gun shoots out an bullet and that bullet need to damage something

okei i still failed but i made it work in a diffrent way