Trouble accessing class from another script

Hi guys/gals,

In script-B I’m attempting to copy a reference to custom class (or variable of type custome class - not really sure what the correct terminology here is) in script-A. When I do, I get an error that states Cannot convert ‘Drift_Abilities’ to ‘Drift_Abilities’ (the custom class, as you may have guessed it called, “Drift_Abilities”).

In script-A and in script-B, I’ve got the custom class defined as:

class Drift_Abilities extends System.ValueType{
	var GuidedMissile	: boolean;		
	var Drainage		: int;			
	var Barren			: int;			
	var Radius			: float;		
	var Zombie			: int;			
	var Strain			: int;			
	
 
  public function Drift_Abilities(GuidedMissile:boolean, Drainage:int, Barren:int, Radius:float, Zombie:int, Strain:int){
	this.GuidedMissile = GuidedMissile;
	this.Drainage = Drainage;
	this.Barren = Barren;
	this.Radius = Radius;
	this.Zombie = Zombie;
	this.Strain = Strain;

   }
}

In script-A and script-B I also have the variable declaration and instance (?) as:

//[outside any functions]
var DA : Drift_Abilities;

//[in the start function]
DA = new Drift_Abilities();

then in script-B I attempt to copy the class that is in script-A with the following:

DA = GameObject.FindGameObjectWithTag("GameController").GetComponent(GameController).DA;

the “path” GameObject.FindGameObjectWithTag(“GameController”).GetComponent(GameController) seems to be reasonable as I can copy a single element in the class for example this works:

DA.Barren = GameObject.FindGameObjectWithTag(“GameController”).GetComponent(GameController).DA.Barren;

but I would really rather be able to access the entire class information rather then one element at a time.

I don’t know if Unity supports the intent that I’m after??
I also know that I’m not currently savvy in creating a custom class and "include"ing it in different scripts.

Looking forward to hear your feedback(s).

Thank you very much.

If I understand correctly you can achieve what you’re trying to do in this way:

  1. Don’t include the class definition for Drift_Abilities in both scripts A and B.

  2. You don’t have a default constructor for Drift_Abilities (one that takes no arguments) so you need to use the one you’ve defined.

  3. Therefore, I would only define drift abilities in one file, and then make an instance using the constructor with arguments, like this:

    public var DA : Drift_Abilities = new Drift_Abilities(
    true,
    5,
    10,
    2.5f,
    0,
    0);

You’d then make an instance of Drift_Abilities in script B, using alternate arguments. Then you could continue as you were saying – get the component in question and assign DA

Correct me if I’m wrong but should that last line be:

DA = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().DA;

To Xtro and AndyMartin458,

Hey guys (assuming male ?), anyways…

thank you both for the extra feedback and suggestions on books (e or paper).
I’ve started a tutorial that I believe Xtro turned me onto, and the book that Andy linked to looks really thorough and I hope to be able to get in hard copy, I’m still one foot in the past with the tactile and highlighting and being able to steady the foot of a severely unbalanced table ( lol ).

Thanks again.

vvvvvvvvvvvvvvvvv OLDER COMMENT BELOW vvvvvvvvvvvvvvvvvvvvv

To Xtro (again),

I still can’t add any more comments, I guess I’ll have to learn to be less verbose (yeah, good luck)…anyway here is my comment to your last post

AWESOME! Xtro!

That did the trick. I’ll have to do a post mortem on what I was doing wrong tomorrow as my eyes have had it for today.

How funny that your post is voted down, but is the ANSWER :slight_smile:

Thank you very much.

vvvvv OLDER COMMENT BELOW vvvvvv

To Xtro,

I wasn’t able to add any additional comments so, I hope I’m not breaking too many rules by posting my comment to you as an answer.

Thanks again,

Yes, I would rather make a reference than a copy into new memory.
From way back I would have called what I’m trying to do is to create a “pointer” in script-B back to the original memory location created by script-A.

I’ve tried a couple of the other examples below except the last one by dood (thank you dood for your effort, but I know I’m just missing something small and I would rather not create yet a couple more scripts, again thank you).

I even tried a C# example the I had found on a different question where the author talked about a “Global” script in which he put all of his custom classes and was able to access them from all of his scripts. I copied verbatim but I think there might have been a little assumption made, cause I’m still getting errors that reflect that script-B is still not seeing the class and so can’t use it as a type.

If you have any more time to spend on this I would certainly like to see 2 script where one has the Class and the other can reference it.

I can do it for single variable of primitive type int, float etc., but not for my custom class.

Am determined though, so I’ll go sharpen my machete.

Thank you again.

Your question and your comments are very detailed and organized. Congratz for that :slight_smile:

If you are interested in OOP, I would definitely suggest you to start coding with C#. As a BSEE you shouldn’t have a problem with C#. I think unity developers has included javascript for the low profile programmers (My apologies to javascript developers but this is my idea). I wouldn’t suggest a book because the internet killed the books. I think you should search for OOP tutorial videos.

About the problem here… Copying the internal data and referencing the same data from different places are completely opposite.

In C# (and many other languages) classes(objects) are referenced, structs(valuetypes) are stored.

A Referenced object occupies only one memory location and its location reference is used in other places to reach the same exact data(like a street address)
ClassType A = new ClassType();
ClassType B = A;
in here A and B just holds the street address of the occupied memory zone by new command. If you change a member value of A, B also changes.

Each Struct occupies the own memory zone on where it’s defined. If you define 2 variables with the same type, they each has their own memory.
StructType A; // generally this is enough to define a struct since it has it’s own memory. There is no need to create it.
StructType B; // this occupies a second memory for its own.
B=A; // this copies all the internal data from one memory block to another. There is no link between them. If you change one, the other doesn’t change. I’m talking about C#. Don’t know about this detail on javascript.

OOP contains many many other concepts other than this memory stuff. You don’t have to learn OOP for your this specific problem.

Just decide that if you want a centralized data object referenced by many places or if you want more than one distributed and auto-copied data structs.

Auto copying can be achieved on referenced objects by declaring a special method and manually copying the internal data in it. But you would have to call that special method instead of equals symbol.
B = A.Copy(); or ClassType.Copy(A, B);

Edit for your decision which is a referenced object :

Don’t extend your type from ValueType. Don’t extend it at all. So your type will be a class, not a struct. Actually it inherits the System.Object class but you don’t have to declare it.

class Drift_Abilities{
    var GuidedMissile  : boolean;     
    var Drainage     : int;      
    var Barren        : int;         
    var Radius        : float;   
    var Zombie        : int;         
    var Strain        : int;         
 
 
  public function Drift_Abilities(GuidedMissile:boolean, Drainage:int, Barren:int, Radius:float, Zombie:int, Strain:int){
    this.GuidedMissile = GuidedMissile;
    this.Drainage = Drainage;
    this.Barren = Barren;
    this.Radius = Radius;
    this.Zombie = Zombie;
    this.Strain = Strain;
 
   }
} 

Now this type is a class. The instance of this class is a referenced object. After a “B = A;” line, both A and B will reference the same object so any change done in A will reflect to B.

If you want to add a Copy functionality to this class, it would be something like this…(I have to write it in C#)

public Drift_Abilities Copy(){
    Drift_Abilities Result = new Drift_Abilities();
    Result.GuidedMissile = GuidedMissile;
    Result.Drainage = Drainage;
    // ...
    // ...

    return Result;
}

So this creates a copy of it in a new memory location and passes the new locations reference. You would use it like…

B = A.Copy();