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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating a Score Display Error 00002

Discussion in 'Scripting' started by Carter0, Jun 1, 2016.

  1. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    I am trying to create a score marker for my space shooter game. I ran into this error I have seen before, but I still don't know how to fix it. Can someone help me?

    The error is,

    "Assets/Scripts/DestroyByContact.cs(7,17): error CS0246: The type or namespace name `GameController' could not be found. Are you missing a using directive or an assembly reference?"



    Here is my code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByContact : MonoBehaviour
    5. {
    6.     public int scoreValue;
    7.     private GameController gameController; //The error shows up here.
    8.  
    9.  
    10.     void Start ()
    11.     {
    12.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    13.         if (gameControllerObject != null)
    14.         {
    15.             gameController = gameControllerObject.GetComponent <GameController>();
    16.         }
    17.         if (gameController == null)
    18.         {
    19.             Debug.Log ("Cannot find 'GameController' script");
    20.         }
    21.     }
    22.  
    23.  
    24.     void OnTriggerEnter2D (Collider2D other)
    25.     {
    26.  
    27.         if (other.tag == "Boundary")
    28.         {
    29.             return;
    30.         }
    31.  
    32.         Destroy (other.gameObject);
    33.         Destroy(transform.gameObject);
    34.  
    35.         gameController.AddScore (scoreValue);
    36.         Destroy(other.gameObject);
    37.         Destroy(gameObject);
    38.     }
    39. }
    Thank you for the help!
     
  2. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Try removing the private
     
    Carter0 likes this.
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Also your GameObject shouldn't be used for Controllers say instead:

    Code (CSharp):
    1. void Start()
    2. {
    3.        gameController = GameObject.FindGameObjectsWithTag("tag").GetComponent<gameController>();
    4. }
     
    Carter0 likes this.
  4. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Removing the private did not fix the issue and I fixed the code like you suggested. Thanks for the help! Do you have any other ideas?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The access modifier and usage of GetComponent have absolutely nothing to do with this error message.

    Show us how your GameController class is defined.
     
    Carter0 likes this.
  6. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    I think I may have just realized the problem after your comment. I don't have a GameController class. Ill post again if I need more help, but thank you for the help!
     
  7. Carter0

    Carter0

    Joined:
    Feb 12, 2016
    Posts:
    62
    Yeah that made the code work. I still have some more work to do, but changing class names around fixed the issue!