Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Referencing another script?

Hey guys, i'm currently trying to create a way for my player to follow moving objects by clicking on them and i believe i have got that down, however I've been trying to reference my players "PlayerMovement" script, so once i have started to focus on an object by clicking on it, my players "PlayerMovement" script will start following the target i have set.


I have it so my script requires the "PlayerMotor" script by [RequireComponent(typeof(PlayerMotor))]

and i set what motor means by doing "motor = GetComponent<PlayerMotor>();" inside the void Start() function.

anyone know how to fix this? Hopefully i haven't been too vague.

Best Answers

  • MrSlugMrSlug Member
    Accepted Answer

    @AtticusDraxton,

    Oh! I see the problem, you're not declaring the motor variable correctly.

    Make the following change:

    public Interactable focus;
    private PlayerMotor motor;
    

    And this will fix the error.

  • MrSlugMrSlug Member
    Accepted Answer

    @AtticusDraxton,

    You don't need to change that, just make the change I suggested and it's available to your whole script.

    PlayerMotor motor = GetComponent<PlayerMotor>(); defines the variable locally, meaning you'd need to declare it again every time you want to use it.

  • Accepted Answer

    Yes, so it's best not to have it as a local variable. maybe a private variable at the top.

  • Accepted Answer

    or a public variable to assign in the inspector

Answers

  • MrSlugMrSlug Member

    Hi ~AtticusDraxton,

    I'm slightly confused as to what you're hoping to achieve.

    Are you wanting your PlayerMovement script to reference PlayerMotor while on the same gameobject?

    Can you post your code so we can see it in action?

  • Yeah im trying to refrence my PlayerMotor Script that's on the same object as my PlayerMovement Script.

    To make it so my PlayerMovement script takes in all the input while my PlayerMotor Script actually moves the character

    This is the code im using. Sorry it's alot. Im kind of having it also interact with the object it's following too but i know that works just having issues getting it to understand that im refrenceing my PlayerMotor Script to move my player towards what im trying to interact with.


    These are the Error Codes that unitys giving me

    Assets\Scripts\PlayerInteraction.cs(15,9): error CS0103: The name 'motor' does not exist in the current context

    Assets\Scripts\PlayerInteraction.cs(45,9): error CS0103: The name 'motor' does not exist in the current context

    Assets\Scripts\PlayerInteraction.cs(51,9): error CS0103: The name 'motor' does not exist in the current context

    I also marked where the error codes showed up for ya to make them easier to spot.


    This is my PlayerInteraction Script i was calling it PlayerMovement script earlier just to try and make it easier to understand what i was trying to do with less information, and super thanks btw for even trying to help. :)


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;


    [RequireComponent(typeof(PlayerMotor))]

    public class PlayerInteraction : MonoBehaviour

    {


      public Interactable focus;

       

       

      // Start is called before the first frame update

      void Start()

      {

        motor = GetComponent<PlayerMotor>(); (1st Error Code)

      }


      // Update is called once per frame

      void Update()

      {

        if (Input.GetMouseButtonDown(1))

        {

          Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);

          RaycastHit hitInfo;


          if (Physics.Raycast(myRay, out hitInfo, 100))

          {

            Interactable interactable = hitInfo.collider.GetComponent<Interactable>();

            if (interactable != null)

            {

              SetFocus(interactable);

            }

          }

        }


        if (Input.GetMouseButtonDown(0))

        {

          RemoveFocus();

        }

      }


      void SetFocus (Interactable newFocus)

      {

        focus = newFocus;

        motor.FollowTarget(newFocus); (2nd Error Code)

      }


      void RemoveFocus ()

      {

        focus = null;

        motor.StopFollowingTarget(); (3rd Error Code)

      }

    }


    This is my PlayerMotor Script However i dont think it's the issue i just put it on here just in case.

    (You Can Most Likely Ignore This Part) -----------------------------------------------------------------------



    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.AI;


    [RequireComponent(typeof(PlayerInteraction))]

    [RequireComponent(typeof(NavMeshAgent))]

    public class PlayerMotor : MonoBehaviour

    {

      Transform target; //target to follow

      NavMeshAgent agent; //reference to our agent


      // Start is called before the first frame update

      void Start()

      {

        agent = GetComponent<NavMeshAgent>();

      }


      void update ()

      {

        if (target != null)

        {

          agent.SetDestination(target.position);

        }

      }


      public void MoveToPoint(Vector3 point)

      {

        agent.SetDestination(point);

      }


      public void FollowTarget (Interactable newTarget)

      {

        target = newTarget.transform;

      }


      public void StopFollowingTarget ()

      {

        target = null

    }

    }

  • thanks a bunch MrSlug!

  • motor = GetComponent<PlayerMotor>();

    You didn't define a type for variable motor. Write this:

    PlayerMotor motor = GetComponent<PlayerMotor>();

    This should fix all errors.

  • would i put that inside the start function?

  • Oh, ok thanks guys.

Sign In or Register to comment.