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

Does not exist in current context

Discussion in '2D' started by iningomontoya, Aug 20, 2017.

  1. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    I am referencing a script from another object. I am using the FindObjectOfType<ScoreManager>(); and of course the object is ScoreManager. Yet, I get a does not exist error in connection with the float that I am trying to work with.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using system;
    5. public class PowerupsManager : MonoBehaviour {
    6.  
    7.         private bool doublePoints;
    8.         private  bool safeMode;
    9.  
    10.         private bool powerupActive;
    11.  
    12.         private float powerupLengthCounter;
    13.  
    14.         private ScoreManager theScoreManager;
    15.  
    16.        
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.             theScoreManager = FindObjectOfType<ScoreManager>();
    21.        
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.        
    28.             if(powerupActive)
    29.             {
    30.                powerupLengthCounter -= Time.deltaTime;
    31.  
    32.                if(doublePoints)
    33.  
    34.                {
    35.  
    36.                 theScoreManager.pointsPerSecond = normalPointsPerSecond * 2;
    37.                }
    38.  
    39.                if(powerupLengthCounter <= 0)
    40.  
    41.                 {
    42.                   theScoreManager.pointsPerSecond = normalPointsPerSecond;
    43.                   powerupActive = false;
    44.                 }
    45.              }
    46.        
    47.     }
    48.  
    49.        public void ActivatePowerup(bool points, bool safe, float time)
    50.  
    51.        {
    52.  
    53.         doublePoints = points;
    54.         //safeMode = safe;
    55.         powerupLengthCounter = time;
    56.  
    57.         normalPointsPerSecond = theScoreManager.pointsPerSecond;
    58.        
    59.         powerupActive = true;
    60.        }
    61.  
    62. }
     
  2. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    I have got it to work although I am not sure if my solution is the best one. I changed the float that it was referencing from a private to a public float and changed normalPointsPerSecond to theScoreManager.normalPointsPerSecond.
     
  3. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Your solution is correct, at least that's how I would pass the float. If you keep it as a private float, it is only accessible within that class, hence the error 'does not exist in current context', which means, the context of that class, and all available public variables.