Global component access

Hi all,

I wish to access my player game object from most scripts and currently do:

Start()
{
player = game object.find(“player”);
}

I know I could use tags etc. But can’t I have some singleton object that ‘finds’ the player and everyone inherits the ‘player’ variable but doesn’t do the global classes start (i.e. call find(“player”) again);
So essentially the above code is done once and once only and everyone else just uses that value.
I guess I kind of need inherited members but not methods.
I realise this is more a c# issue - but any help appreciated

Cheers

Like I mentioned it’s my lack of c# oop that’s an issue.
The only other ‘reasonable’ option I see is to have 1 singleton object that does all it’s find’s on Awake i.e. find player, find ui etc. etc.

Then every other object just does a find on that one object in order to access all the ‘globals’.
I am really trying to avoid find’s of any type on objects which are permanent like the player and ui.
I assumed ‘somehow’ this could be achieved via inheritance but I guess I am wrong.

Cheers

What exactly is the problem with your current approach using find everywhere? It is much simpler to use than any singleton solution, which would require at least some script execution ordering to work at all. If it’s just for performance, don’t bother. There is no way that one GameObject.Find or one FindWithTag will visibly drop your frame rate, especially if they’re only used in start.

Hi,
Well that was my exact point.
I understand that find/findtag are hardly going to kill my fps relative to 10,000 ploys :slight_smile: , but it just seemed ‘wasteful’.
The singleton idea is like a mega headache I agree.
It’s just that some of my objects might need to access the player object and then various components of that object aswell; for many many objects.
I guess the simplest nice solution is to have 1 initial object which does all the finds I could need and everyone just accesses that with just once find tag (as that’s faster than plain find.)

I just assumed there was a reasonable solution.

Cheers

What we occasionally do, is have a script on a player or ai character that just contains references to all the important scripts on that particular GameObject. Such approach scales much better than any singleton/static variable based solution. You’d still need to find the player object, but after that you only need that single script component to get the other links.

Hey tomvds,

I see what you mean.
But my method I just find my “global access class” which has found all the ‘common’ game objects and components that exist permanently and then any object just finds that “global access object” and grabs what it wants.
I just feel it’s ‘cleaner’ as any object that wants the player object, or player scripts or ui or whatever can access just 1 point.

Cheers

It’s certainly doable. They key thing to remember is that a class doesn’t have to inherit MonoBehaviour to interact with the scene. One thing that would be fun (although maybe slightly overblown) is to also return a bool indicating whether or not the object is present in the scene.

private GameObject thePlayer;

public bool GetThePlayer(out GameObject player)
{
    player = thePlayer;
    return thePlayer != null ? true : false;
}

Then you could do

GameObject playerRef;
if (GetThePlayer(out playerRef)
{
    // do some stuff to the player because we know he isn't null
}

This is sort of a bad example (and I’m not saying it should be implemented) because GameObject implements the bool operator that checks for existence already.

Hey KeloMRK,

I guess so :slight_smile:

I just did a global singleton that finds game objects and scripts.
Then any other game objects just find my global singleton - nice and simple; works as expected :-))))

Cheers