Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Why can't I access certain scripts from other scripts?

Discussion in 'Scripting' started by faulknordonald, Apr 25, 2024.

  1. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    212
    I am working with the FPS Microgame and I can't access scripts I created from within the FPS Microgame Scripts.

    I created this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.FPS.Game;
    5.  
    6.     public abstract class XPTranslation : ScriptableObject
    7.     {
    8.         public int CurrentXP { get; protected set; } = 0;
    9.         public int CurrentLevel { get; protected set; } = 1;
    10.         public int XPRequiredForNextLevel => GetXPRequiredForNextLevel();
    11.         public bool AtLevelCap { get; protected set; } = false;
    12.  
    13.         public abstract bool AddXP(int amount);
    14.         public abstract void SetLevel(int level);
    15.  
    16.         protected abstract int GetXPRequiredForNextLevel();
    17.     }
    FPS Microgame created this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. namespace Unity.FPS.Game
    5. {
    6.     public class Health : MonoBehaviour
    7.     {
    8.         XPTranslation XPTranslation; // I created this line, which does not access my script.
    9.         [Tooltip("Maximum amount of health")] public float MaxHealth = 10f;
    10.  
    11.         [Tooltip("Health ratio at which the critical health vignette starts appearing")]
    12.         public float CriticalHealthRatio = 0.3f;
    13.  
    14.         public UnityAction<float, GameObject> OnDamaged;
    15.         public UnityAction<float> OnHealed;
    16.         public UnityAction OnDie;
    17.  
    18.         public float CurrentHealth { get; set; }
    19.         public bool Invincible { get; set; }
    20.         public bool CanPickup() => CurrentHealth < MaxHealth;
    21.  
    22.         public float GetRatio() => CurrentHealth / MaxHealth;
    23.         public bool IsCritical() => GetRatio() <= CriticalHealthRatio;
    24.  
    25.         bool m_IsDead;
    26. /// Lines Continue...
    I even tried creating a namespace around my script:
    upload_2024-4-25_7-28-18.png

    Which still doesn't work:
    upload_2024-4-25_7-29-16.png

    I can work in reverse. If I added Health to the XPTranslation script, it works fine.

    Any help would be greatly appreciated. Thank You.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,432
    Does the micro game use assembly definitions? Are you using assembly definitions?
     
    SisusCo and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,219
    Besides the potential use of assembly definition files, it would be interesting to know where in the assets folder the two scripts are located. There are still magic folder names which can get in the way.
     
    PraetorBlue and spiney199 like this.
  4. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    212
    I have no clue. How do I find out about that?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,949
  6. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    212
    upload_2024-4-25_11-56-14.png
    Health Script provided by FPS Microgame

    upload_2024-4-25_11-57-10.png
    The scripts I am trying to access in the Health script.

    Right now, I have a working system to gain experience and increase levels. What I am trying to do is increase stats with every level increase. One of the stats would be health. I am trying to increase the player health when leveling up.
     
  7. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    212
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,219
    An assembly definition file essentially separates the subfolder it is in and all nested folder into a separate assembly. So the code in that folder (and subfolders) are compiled to a completely separate standalone DLL / assembly file.

    Unity, by default, when not using any assembly definition files will compile all your code into a single assembly which is called
    Assembly-CSharp.dll
    . This is somewhat your main assembly. This will automatically reference your other assemblies. However it can not be referenced by your standalone assemblies as you can not have circular dependencies between assemblies. So it's not possible to have assembly A reference / depend on assembly B and at the same time have B depend on A.

    There are several ways how things like that can be resolved. One way is to get rid of the assembly definition file so all your code is again compiled to the same assembly. When all code is in the same assembly, every class can see and interact with any other class within the same assembly.

    If the mini game should be a separate unit that runs on its own, you could define interfaces that the main code uses / supplies to the minigame in order to have the minigame interact with your main code in an abstracted way. This of course requires a careful planning and to keep the cross dependency to a minimum..

    It's up to you how you plan to design your codebase.
     
    Last edited: Apr 25, 2024
    SisusCo and Ryiah like this.
  9. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    212
    Thanks for the clarification. I don't see any reason why the microgame would have to run separate from anything else, so I should just delete the Assembly definitions then? Everything I'm working on will be built upon this microgame rather than separate entities.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,432
    You can either delete it, or make an assembly definition for your own code and reference the micro game and any other assemblies it's dependent on.
     
  11. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    212
    Ok. Thank you for your help.

    Just curious though, what purpose would assembly definitions serve? Let's say the code is compiled separately. What does that accomplish? Is there an actual game you can use for an example? It sounds like something I might want to look into further.
     
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,432
    Could just read the manual on these things: https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

    In general, it helps with script recompilation. A lot of folks also find it helps with organising code. Personally I find it's good for keeping my code dependencies one-way. I also just like a well-organised project myself.

    It's all behind the scenes stuff, though. And most Unity games I poke around in the files of aren't using them. Even really big Unity games like the Pathfinder games.
     
    Ryiah likes this.