Search Unity

Problem with MonoBehaviour

Discussion in 'Scripting' started by arcticgamesdev, Jul 25, 2019.

  1. arcticgamesdev

    arcticgamesdev

    Joined:
    Jul 25, 2019
    Posts:
    3
    I have been following this tutorial: shorturl.at/bvxz4

    In the WorldInteraction script he his teaching us to right. I have this line of code
    public class WorldInteraction : MonoBehaviour
    the "MonoBehaviour" however is not lit up and whenever I try to put this script into the player I get an error saying "Can't add script behaviour AssemblyInfo, The script needs to derive from MonoBehaviour". Any help with this would be appreciated!

    FULL CODE:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. public class WorldInteraction : MonoBehaviour
    6. {
    7.     UnityEngine.AI.NavMeshAgent playerAgent;
    8.  
    9.     void Start()
    10.     {
    11.         playerAgent = GetComponent<NavMeshAgent>();
    12.     }
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    19.             GetInteraction();
    20.     }
    21.  
    22.     void GetInteraction()
    23.     {
    24.         Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.         RaycastHit interactionInfo;
    26.         if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
    27.         {
    28.             GameObject interactedObject = interactionInfo.collider.gameObject;
    29.             if (interactedObject.tag == "Interactable Object")
    30.             {
    31.                 Debug.Log("Interactable interacted.");
    32.             }
    33.             else
    34.             {
    35.                 playerAgent.destination = interactionInfo.point;
    36.             }
    37.  
    38.         }
    39.     }
    40.  
     
  2. Ossi101

    Ossi101

    Joined:
    Nov 16, 2013
    Posts:
    11
    You're missing the closing curly brace for your class at the end of the script.
     
  3. arcticgamesdev

    arcticgamesdev

    Joined:
    Jul 25, 2019
    Posts:
    3
    That fixed the original problem. Thank YOU! I can't believe I missed it but stuff happens.

    Now I am having a problem where whenever I start the game my character just disappears. He doesn't fall threw the ground. He just vanishes. Any thoughts?
     
  4. arcticgamesdev

    arcticgamesdev

    Joined:
    Jul 25, 2019
    Posts:
    3
    NVM I got it! Thanks again!