heya, same first-time coder here, trying to figure out what is causing my code to fail

for context, I’m using multiple systems of damage, rigidbody bullets, particle missiles, and now, linerender lasers

this is the code in question

LASER:

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

public class sidelaser : MonoBehaviour
{
[SerializeField] private LineRenderer _beam;
[SerializeField] private Transform _muzzlePoint;
[SerializeField] private float _maxLength;
[SerializeField] private float _damage= 2;


private Vector3 hitPosition;

private void Awake() {
_beam.enabled = false;
}
private void Activate() {
_beam.enabled = true;
}

private void Deactivate() {
_beam.enabled= false;
_beam. SetPosition(0, _muzzlePoint.position);
_beam. SetPosition(1, _muzzlePoint.position);
}

private void Update() {  
if(Input.GetKeyDown(KeyCode.Mouse0)) Activate();
else if (Input.GetKeyUp(KeyCode.Mouse0))  Deactivate ();
}

private void FixedUpdate () {
if(!_beam.enabled) return;

RaycastHit target;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out target))
        {
      hitPosition = target.point;
        }

if( ray && hitPosition.TryGetComponent (out MineHealth minehealth))
{
minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
}

_beam. SetPosition(0, _muzzlePoint.position);
_beam. SetPosition(1, hitPosition);
}

THE MINE(tagged "enemy)

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

public class MineHealth : MonoBehaviour

{

    public float minehealth = 100;

    public void AddHealth(float amount)
    {  

minehealth += amount;

    }
public void DecreaseHealth (float amount)
{

minehealth -= amount;
if(minehealth < 0)
{
minehealth = 0;
}
if(minehealth ==0) {
    Destroy(gameObject);
}
}

right now, I’m getting a few error messages, if anyone would be able to help me, that would be lovely!

thank you all in advance

Heya first time coder! Could you edit your post and put your source code into proper Source Code element so it will be displayed right? Thank you.

I assume you mean as such?

laser

mine

No, dont share screenshots, but edit your original post and from the editor top panel you see Insert Code and Inline Code tags. For shorter stuff use inline, but for longer posts use the Insert Code function and paste your code there.

Use code tags when you post code in the forum. More info here .

there, think I did it now!

Thank you. You can also fix your code indentation but now it’s at least readable.

Your error is a type error: you’re calling TryGetComponent using variable hitPosition which is of type Vector3. TryGetComponent is a Component API method.

Now, read the error message.
hitPosition = target.point;
You’re putting a Vector3 value in the hitPosition and then try to get a Component on it TryGetComponent. Vector3 doesn’t have components other than the x, y and such.
You probably want to use the TryGetComponent on the target variable.

ah, that makes sense, what would you recommend I do instead- again, I really dont have a clue myself, tried to adapt code I found as best I could, but I’m really in the deep end here

Ok I think you’re trying to extract your MineHealth component from the object that you hit with raycast. Then you can use target.collider.TryGetComponent. ps your if (ray) is redundant because it will always be true.

Btw I would rather name ‘target’ to ‘hit’ because it’s the RaycastHit object. Accurate naming is very important, first time programmer.

so, basically

hit.collider.TryGetComponent()
{
minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
}

?

Well try it! RaycastHit is not a component, but Collider is.

nope, didn’t work. Gave these three errors:

though I changed it to hit.collision, since the thing I’m trying to target isn’t set as a trigger

Try

if( ray && target.transform.TryGetComponent (out MineHealth minehealth))
{
      minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
}

will try!

sadly enough didn’t work

Like the documentation shows in its example you need to specify a type between the inequality symbols.

if (ray && hitPosition.TryGetComponent<MineHealth>(out MineHealth minehealth))

https://docs.unity3d.com/ScriptReference/Component.TryGetComponent.html

this seemed to fix some errors, yet I’m still left with these:

Ouch, I missed that. Although the Vector3 still won’t have that, so the proper solution should be:

if( ray && target.transform.TryGetComponent<MineHealth>(out MineHealth minehealth))
{
      minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
}

thanks for trying to help, but I still got these issues