Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by leviveenvander, Jul 12, 2023.

  1. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    for context, I'm using multiple systems of damage, rigidbody bullets, particle missiles, and now, linerender lasers

    this is the code in question

    LASER:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class sidelaser : MonoBehaviour
    6. {
    7. [SerializeField] private LineRenderer _beam;
    8. [SerializeField] private Transform _muzzlePoint;
    9. [SerializeField] private float _maxLength;
    10. [SerializeField] private float _damage= 2;
    11.  
    12.  
    13. private Vector3 hitPosition;
    14.  
    15. private void Awake() {
    16. _beam.enabled = false;
    17. }
    18. private void Activate() {
    19. _beam.enabled = true;
    20. }
    21.  
    22. private void Deactivate() {
    23. _beam.enabled= false;
    24. _beam. SetPosition(0, _muzzlePoint.position);
    25. _beam. SetPosition(1, _muzzlePoint.position);
    26. }
    27.  
    28. private void Update() {  
    29. if(Input.GetKeyDown(KeyCode.Mouse0)) Activate();
    30. else if (Input.GetKeyUp(KeyCode.Mouse0))  Deactivate ();
    31. }
    32.  
    33. private void FixedUpdate () {
    34. if(!_beam.enabled) return;
    35.  
    36. RaycastHit target;
    37.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    38.  
    39.         if (Physics.Raycast(ray, out target))
    40.         {
    41.       hitPosition = target.point;
    42.         }
    43.  
    44. if( ray && hitPosition.TryGetComponent (out MineHealth minehealth))
    45. {
    46. minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
    47. }
    48.  
    49. _beam. SetPosition(0, _muzzlePoint.position);
    50. _beam. SetPosition(1, hitPosition);
    51. }

    THE MINE(tagged "enemy)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MineHealth : MonoBehaviour
    6.  
    7. {
    8.  
    9.     public float minehealth = 100;
    10.  
    11.     public void AddHealth(float amount)
    12.     {  
    13.  
    14. minehealth += amount;
    15.  
    16.     }
    17. public void DecreaseHealth (float amount)
    18. {
    19.  
    20. minehealth -= amount;
    21. if(minehealth < 0)
    22. {
    23. minehealth = 0;
    24. }
    25. if(minehealth ==0) {
    26.     Destroy(gameObject);
    27. }
    28. }

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

    upload_2023-7-12_18-32-59.png


    thank you all in advance
     
    Last edited: Jul 12, 2023
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    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.
     
    Bunny83 likes this.
  3. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    I assume you mean as such?

    laser
    upload_2023-7-12_18-57-20.png

    mine
    upload_2023-7-12_18-57-44.png
     
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    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.
     
    Bunny83 likes this.
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Use code tags when you post code in the forum. More info here.
     
  6. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    there, think I did it now!
     
    Lurking-Ninja and KillDashNine like this.
  7. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    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.
     
    Bunny83 likes this.
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    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.
     
  9. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    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
     
  10. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    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.
     
  11. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    so, basically

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

    ?
     
  12. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Well try it! RaycastHit is not a component, but Collider is.
     
  13. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    nope, didn't work. Gave these three errors:

    upload_2023-7-12_19-50-29.png

    though I changed it to hit.collision, since the thing I'm trying to target isn't set as a trigger
     
  14. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Try
    Code (CSharp):
    1. if( ray && target.transform.TryGetComponent (out MineHealth minehealth))
    2. {
    3.       minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
    4. }
     
  15. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
  16. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    sadly enough didn't work
     
  17. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
  18. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15
    this seemed to fix some errors, yet I'm still left with these:

    upload_2023-7-12_20-33-41.png
     
  19. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Ouch, I missed that. Although the
    Vector3
    still won't have that, so the proper solution should be:
    Code (CSharp):
    1. if( ray && target.transform.TryGetComponent<MineHealth>(out MineHealth minehealth))
    2. {
    3.       minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
    4. }
     
  20. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15

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

    upload_2023-7-12_20-42-46.png
     
  21. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Okay, I see you don't want to fix anything in your code...
    Try this, I haven't tested it, since I don't have the rest of your code, so it may not work...
    Code (CSharp):
    1.         private void FixedUpdate () {
    2.             if(!_beam.enabled) return;
    3.  
    4.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.             if (Physics.Raycast(ray, out RaycastHit target))
    6.             {
    7.                 if (target.transform.TryGetComponent<MineHealth>(out MineHealth minehealth))
    8.                 {
    9.                     minehealth.DecreaseHealth(_damage*Time.fixedDeltaTime);
    10.                 }
    11.                 _beam. SetPosition(0, _muzzlePoint.position);
    12.                 _beam. SetPosition(1, target.point);
    13.             }
    14.         }
    15.  
     
    leviveenvander likes this.
  22. leviveenvander

    leviveenvander

    Joined:
    Jul 10, 2023
    Posts:
    15

    This ended up being the fix! Thank you for taking the time to help me (and I would love to fix my code, I just don't have the current skills to know WHAT I would need to fix, regardless, thank you)