Or is that only used when I am instantiating an object?
The kinds of things that are happening are variables that I am reading via a reference to an object are not being updated (even though I can see the in the instance they are), I think it’s because I have a reference to the prefab and not the instance of the object.
This is where I’m getting confused about how to use GameObject.Find - does it only read heirarchy objects or also project prefabs?
Also - in the inspector I am dragging prefabs onto the script object references, do I need to do this? As it looks like you do, but it didn’t mention this in the book I’m reading.
Nope. If you have assign a reference, you need not use find again. Unless the gameobject is instantiated at runtime which makes is hard to use a pre assigned reference because things might change. But if the gameobjects exist before runtime, you can just use references. Saves time to find the gameobject.
Firstly to explain, the find method is used to find the gameobject that already existed in your scene. Say i have A B in my scene (2 cubes).
Then in A i use
// Attached to A
var gameControl : GameControl_script;
function Start()
{
gameControl = GameControlGameObject.Find("B").GetComponent("GameControl_script");
// Make a check
if (gameControl == null)
{
print("Error");
}
}
You can also just do this.
// Attached to A
// Make sure you drag your game control to this object in your inspector
var gameControl : Transform;
function start()
{
gameControl.GetComponent("GameControl_script");
}
And next time try searching the forums. I think someone has asked this before about using GameObject.Find().
just to warn you a bit (for performance reasons), try to do not use gameobject.find or transform.find in Update functions,
gameobject.find is really useful to avoid to dragging and drop your gameobject to the inspector, you can just create a private var and inside an Awake function declare your var using GameObject.Find
Thanks for your help on this. I went all through my code and corrected the mess I’d got into (and there was quite a bit) - how it ever worked at all is beyond me.
Then I also noticed the huge glaring error that was causing much of the strangeness. I had assigned two copies of the player_ship script to the player_ship! This explained everything - the messages of unassignments but the game still ran - twice as many missiles as there should be with no array out of range errors.