How do I fix this?
Hi I'm very new to C# script, I really need help because I got a error if you can help please do it would help alot
The error says:
error CS1061: 'Transform' does not contain a definition for 'Position' and no accessible extension method 'Position' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
The code: using UnityEngine;
public class GunScript : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire1"))
{
Shoot();
}
}
void Shoot ()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.Position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}
If you know how to help me please do, It would help me alot
Answer by Captain_Pineapple · Jul 15, 2020 at 03:16 PM
in line
if (Physics.Raycast(fpsCam.transform.Position, fpsCam.transform.forward, out hit, range))
the P
of Position must be lower case.
For future questions please always add the line an error points to. This is crutial information. Also instead of posting edits as answers please edit your question instead.
Answer by carlitos53 · Jul 15, 2020 at 01:57 PM
I also have a second code for the target
code:
using UnityEngine;
public class Target : MonoBehaviour {
public float health = 50f;
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
}
}
void Die ()
{
Destroy(gameObject);
}
}
Please help if you can, Thank you!