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

Problems with accessing a script in another script

Discussion in 'Scripting' started by Hotpots, Oct 23, 2015.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    So I'm using a similar technique with displaying score in one scene in comparison to my previous scene.

    My first scene (attached to the score text gameobject):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class ScoreManager : MonoBehaviour
    7. {
    8.     // The player's current weight.
    9.     public static float currentweight;      
    10.     // Reference to the Text component.
    11.     Text text;                              
    12.  
    13.     void Start()
    14.     {
    15.         // Set up the reference.
    16.         text = GetComponent <Text> ();
    17.  
    18.         // Reset the score.
    19.         currentweight = 0;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         // Set the displayed text to be the word "Weight" followed by the currentweight value.
    25.         if (currentweight == 0)
    26.         {
    27.             text.text = "Weight: 0 kg";
    28.         }
    29.         else
    30.         {
    31.             text.text = string.Format("Weight: {0:#0.0} kg", currentweight);
    32.         }
    33.     }
    34. }
    My second scene (attached to the score text gameobject):

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class AppleScoreManager : MonoBehaviour
    7. {
    8.     // The player's current weight.
    9.     public static int finalapplevalue;
    10.     // Reference to the Text component.
    11.     Text text;
    12.  
    13.     void Start()
    14.     {
    15.         // Set up the reference.
    16.         text = GetComponent <Text> ();
    17.  
    18.         // Reset the score.
    19.         applevalue = 0;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         text.text = string.Format("That Apple is Number {0:#0}", finalapplevalue);
    25.     }
    26. }
    This script is attached to the player in the second scene:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class CollectingApples : MonoBehaviour
    6. {
    7.     public static int applevalue;
    8.  
    9.     bool ismultiple2;
    10.     bool ismultiple3;
    11.     bool ismultiple4;
    12.     bool ismultiple5;
    13.     bool ismultiple6;
    14.  
    15.     GameObject apple1;
    16.     GameObject apple2;
    17.     GameObject apple3;
    18.     GameObject player;
    19.  
    20.     // Use this for initialization
    21.     void Start()
    22.     {
    23.         apple1 = GameObject.FindGameObjectWithTag("apple1");
    24.         apple2 = GameObject.FindGameObjectWithTag("apple2");
    25.         apple3 = GameObject.FindGameObjectWithTag("apple3");
    26.         player = GameObject.FindGameObjectWithTag("Player");
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.     }
    33.  
    34.     void OnTriggerEnter(Collider other)
    35.     {
    36.         if (other.gameObject.tag == "apple1")
    37.         {
    38.             applevalue = 1;
    39.             AppleScoreManager.finalapplevalue = applevalue;
    40.             other.gameObject.SetActive(false);
    41.         }
    42.  
    43.         if (other.gameObject.tag == "apple2")
    44.         {
    45.             applevalue = 2;
    46.             other.gameObject.SetActive(false);
    47.         }
    48.  
    49.         if (other.gameObject.tag == "apple3")
    50.         {
    51.             applevalue = 3;
    52.             other.gameObject.SetActive(false);
    53.         }
    54.     }
    55. }
    Now I'm getting this error and I don't know why? (Line 39)

    The name of my script is... AppleScoreManager
     
  2. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I even tried this in the start function:
    Code (CSharp):
    1. AppleScoreManager AppleScoreManager = player.GetComponent<AppleScoreManager>();
    Although I find it pointless to change my code when I've got the exact same thing working in my previous scene :(
    CONFUSED :/
     
  3. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Try changing the name of the variable so that it doesn't match the name of the class.
     
  4. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Still does not work :(
     
  5. kmgr

    kmgr

    Joined:
    Mar 5, 2015
    Posts:
    9
    You're not declaring the variable 'AppleScoreManager' anywhere, so it doesn't exist.
     
  6. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I'm so confused, I didn't have to declare any script as a variable previously.
    If I wanted to access a variable I would just do something like this:

    Code (CSharp):
    1. ScoreManager.currentscore += score;
    Why wouldn't that work now?


    Like so here in my other scene:

    Code (CSharp):
    1.     void OnTriggerStay(Collider other)
    2.     {
    3.         // If the entering collider is a book...
    4.         if (other.gameObject.tag == "book" && Input.GetKey("e"))
    5.         {
    6.             itemweight = 1.2f;
    7.             ScoreManager.currentweight += itemweight;
    8.             other.gameObject.SetActive(false);
     
  7. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Still stuck on this problem :(
    Help is really appreciated.
     
  8. kmgr

    kmgr

    Joined:
    Mar 5, 2015
    Posts:
    9
    You're missing the context. You have to declare a variable of a type (AppleScoreManager int this situation) to be able to use it in the current scope.

    I don't want to sound rude, but this is basic programming stuff, so maybe you need to follow a tutorial about classes, objects, declaring them and creating references.
     
  9. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I know exactly what your saying kmgr, and that was just an honest error.
    However as you can see in my previous ObjectPickup script I did not reference the ScoreManager script in my ObjectPickup script and I was able to use it in my ObjectPickup script.

    EDIT: I managed to find a solution :) Thanks for the help.