Accessing object variable from another object (classes, not GameObject)

Not sure how to actually phrase the question.

In UnityScript, if I have a class like this

var someObj = someClass();
var anotherObj = anotherClass();

class someClass
{
    var name : String;
    function someClass()
    {
        this.name = "TestName";
    }
}

class anotherClass
{
    var name : String;
    function anotherClass()
    {
        this.name = someObj.name;
    }
}

Why can’t I access the other objects variable “name” from within this, since someObj is within the scope of the script. Obviously my actual script is a bit more detailed, but before I keep going in the path I am going with it, I wonder if I should approach it from a different angle. Basically, I am writing a gui class that will have multiple methods within it (called by the main OnGui function) and other classes within the script that control various values within the game, but I will need the gui class object to be able to access the other objects

Note : When I am talking about objects here I do not mean gameObjects. I mean actual programming objects, or instances of the class.

Edit: Actually, I won’t be doing this because it’s really bad practice, but now, out of curiosity, is it actually possible?

Your anotherClass() method is referencing an instance of an object that is being created at runtime, which it can’t do. An alternative solution would be to make the anotherClass constructor take a string input, like so:

class anotherClass 
{
    var name : String;
    function anotherClass(var newName : String) 
    {
        this.name = newName;
    }
}

and then to pass the desired name into the constructor:

var someObj = someClass();
var anotherObj = anotherClass(someObj.name);

Well, as a workaround to make this piece work, you can try the following:

    class objectPlaceHolder {
         static var someObj = someClass();
    }

    var anotherObj = anotherClass();
     
    class someClass
    {
        var name : String;
        function someClass()
        {
            this.name = "TestName";
        }
    }
     
    class anotherClass
    {
        var name : String;
        function anotherClass()
        {
            this.name = objectPlaceHolder.someObj.name;
        }
    }

In C# I guess it would be easier to make it work without this workaround.

If anotherClass lifetime is supposed to include someObj lifetime, you can just make someObj member of anotherClass.