Move Within Range

I keep getting this error in my code. I have followed the video word for word.
this is the code:
public class Fighter : MonoBehaviour

{

  Transform target;

  

  private void Update() 

  {

     if(target != null)

     {

        GetComponent<Mover>().MoveTo(target.position);

     }

  }

    

      public void Attack(CombatTarget combatTarget)

     {

        this.CombatTarget = combatTarget;

        this.combatTarget = combatTarget;

         target = combatTarget.transform;

     }

        

}

}

The issue lies in the public void Attack(CombatTarget combatTarget)

1 Like

Hi!

I compared your code with the one on the repo, you have two extra lines of code that are doing some weird things.

this.CombatTarget = combatTarget;

this.combatTarget = combatTarget;

Erase them and see if that solves your problem, it should since the error says you are trying to access something you can’t, which is CombatTarget.

1 Like

Hi.

I’m not sure how that happened but no, i deleted them and i didn’t fix it. I’ve used CombatTarget in the PlayerController so not sure why it won’t work.

1 Like

Then it probably has something to do with the namespaces. Check if both your CombatTarget class and Fighter class are both within the RPG.Combat namespace.

1 Like

The Fighter.cs is in RPG.Combat and the PlayerController.cs is in RPG.Controls.

In the video he only uses RPG.Movement to call to the Mover class. I could try using RPG.Controls to see if that fixes it. I don’t know why the dude in the video didn’t come across this same issue though.

edit: using RPG.Control didn’t fix it either.

1 Like

The error says that you are trying to access CombatTarget but you can’t. So the issue is within the CombatTarget class, it is either private, or not within the Combat namespace. So check the CombatTarget class, meanwhile I’ll try to replicate the error.

1 Like

It’s definitely inside of the combat namespace. I will post a picture on the CombatTarget.cs


We haven’t started working in CombatTarget yet.

1 Like

There’s your issue, your class is set as private, set it to public:

public class CombatTarget
1 Like

That fixed it!! FInally. That problem annoyed me for 5 hours. Thanks for the help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms