Home > Net >  How to call a class in Unity, without assigning the script to a gameObject
How to call a class in Unity, without assigning the script to a gameObject

Time:09-02

I have a gameController GameObject, which pulls in and creates 3 List objects built from text files (NameBoys, NameGirls, NameLast). I have defined a Name Class, which is defined as as 3 strings (first, middle, last). In the gameController GameObject, I want to create a function that returns an object of my custom Name type, which will then be used my a NPC type on screen unit. How can I reference the custom Name class in both the gameController GameObject as well as the NPC GameObject, without assigning the script to a gameObject and referencing the GameObject from both places?

CodePudding user response:

  1. You should have a script on gameController, which controlls your name class.

  2. NPC should also have a script, with a field open to script of type gameController.

  3. On NPC, you can reference this class (and any of it's contents) by dragging the class on gameController onto the script on NPC.


Notes:

  • NPC will have all data gameController has, updated every frame

CodePudding user response:

Attempting to explain my issue to Walter showed me the answer. Just add a namespace to your custom class, and you can reference it normally like you would in any other C#.net application, with a 'using' clause.

  • Related