Way to return a class?

So, I just found out that in WebGL you can’t use strings to access things in get component. ( WHAT A FOOL I WAS FOR REMOVING PRAGMA STRICT!!!)

rock.GetComponent("RockScript").hardness = 10;
//This doesn't work in WebGL

Now I have to go change like 100 errors in my code. But there’s one big one that I am having trouble finding a practical solution to.

static function findscript (playern : GameObject) {

	if(playern.name == "Xander"){

		 return "move";

	}else if(playern.name == "Danny"){

		return "move1";

	}

}

I run this function a lot. I always use it like this

player.GetComponent(seek.findscript(player)).dead = true;

Essentially, both players have 2 different scripts with the same variables (no, I cannot make them into the same script). I simply take the game object and, using the name of the player, return the respective name of that player’s script. This way, I don’t have to do a long if statement before every time I want to access one of the move scripts.

But now I need to put a class into GetComponent, not a string. Sadly,

static function findscript (playern : GameObject) {

	if(playern.name == "Xander"){

		 return move;

	}else if(playern.name == "Danny"){

		return move1;

	}

}

Doesn’t work. I get a “type could not be resolved because of a cycle.” error. I’m not the best coder, but to my knowledge, I think this means I the function needs to return the same class each time.

This is a bother because the whole point of this function is to return a different function for each input.

Any ideas how I can practically deal with all of these errors? Thanks, sorry this was such a long question.

Well, you can’t use any dynamic code when you use pragma strict. So your whole approach here is lacking. Having two seperate scripts with the same variable is a bad approach in general. Either seperate that variable into it’s own seperate script which might be used from the other two, or use some sort of interface / base class approach.

It seems UnityScript now supports declaring interfaces. So both of your classes can now implement the same interface. The interface would provide methods to get / set the dead state of the script.

// UnityScript
public interface IDeadState
{
    function GetDead() : boolean;
    function SetDead(state : boolean);
}

// UnityScript
public class move extends MonoBehaviour implements IDeadState
{
    var dead : boolean;
    function GetDead() : boolean
    {
        return dead;
    }
    function SetDead(state : boolean)
    {
        dead = state;
    }
    // [ ... ]
}

To access the script you can simply use GetComponent with the interface. This will return whatever component does implement that interface. I’m not sure if the syntax is right since i usually don’t use UnityScript (it’s a horrible language IMO):

var script : IDeadState = player.GetComponent(IDeadState);
script.SetDead(true);

It’s not clear what those two classes actually do so we can’t say which approach would be better. However i guess simply using a third script is the easiest solution

// DeadState.js

var dead : boolean = false;

You simply attach that script along side to your other two scripts. Instead of having a dead variable in those scripts, you would simply use the one in this script.

// move.js
var deadState : DeadState; // either assign in the inspector or use GetComponent in Start() to assign the reference.

ps: I would also recommend to switch to C# ^^

i know i will get a lot of hate and down vote but maybe you should try c# instead of unity script

with c# you have more control and those kind of problem don’t exist.
You also have a lot more documentation with c#