error CS1061: Type `InventoryGUI' does not contain a definition for `Toggle...

error CS1061: Type InventoryGUI' does not contain a definition for ToggleInventoryWindow’ and no extension method ToggleInventoryWindow' of type InventoryGUI’ could be found (are you missing a using directive or an assembly reference?)

I dont understand this, what have i done wrong?

Script A is to tell Script B to execute function ‘ToggleInventoryWindow’

Script A

if (Input.GetButton ("Inventory"))
		{
			gameObject.GetComponent<InventoryGUI>().ToggleInventoryWindow();
		}

Script B

public void ToggleInventoryWindow() {
		{
			DisplayInventory = !DisplayInventory;
		}
	}

Bonus Question [NOT REQUIRED]
Also is there a better way for one script to interact with another? Or is this fine?

gameObject.GetComponent<InventoryGUI>().ToggleInventoryWindow();

We’d have to see more code for your InventoryGUI. Where is that function in “Script B” located? It needs to be in your InventoryGUI class.

As for the Bonus question… yes… You can do:

private InventoryGUI _inventoryGUI;

Then in your Start function:

_inventoryGUI = gameObject.GetComponent<InventoryGUI>();

That will cache it into the _inventoryGUI variable… then you can just use:

_inventoryGUI.ToggleInventoryWindow();

Cheers!
As for the rest of the InventoryGUI code…

using UnityEngine;
using System.Collections;

public class InventoryGUI : MonoBehaviour {

public bool DisplayInventory = true;

public void ToggleInventoryWindow() {
		{
			DisplayInventory = !DisplayInventory;
		}
	}
}

and thats it (Besides the code that has nothing to do with this problem)

Cant you use

    if (Input.GetButton ("Inventory"))
            {
                gameObject.GetComponent<InventoryGUI>().DisplayInventory = !DisplayInventory;
            }

Assuming DisplayInventory is public which it looks like it is.

Also if you set it up as Dustin Horne suggests then you could use

_inventoryGUI.DisplayInventory = !DisplayInventory;

DisplayInventory is in the other script and yes it is public.

Script A is the player controls script

Script B is the GUI for the Inventory

Hmm… could you try right clicking the script and choose “Sync MonoDevelop Project” ? I don’t see anything wrong with the code but I have this happen occasionally with Visual Studio where I make changes to the script but Unity doesn’t see them until I do the “Sync”.

That worked! Cheers!

However I am greeted by my arch-error! Dun dun duuunnnnnn!!!

NullReferenceException: Object reference not set to an instance of an object
playerMovement.Update () (at Assets/FantasyCraft Assets/playerMovement.cs:75)

I never fully understoud this error (hence why its my arch-error) except that it means something is missing or non existent…but why?
This occurs when i execute the following code which this entire forum is based on.

gameObject.GetComponent<InventoryGUI>().ToggleInventoryWindow();

Because the InventoryGUI component hasn’t been added to your gameObject… either that or it is added after the script that’s trying to access it (not sure if that makes a difference). You may move the InventoryGUI component up to make sure it’s above the script component that’s trying to access it to make sure it’s been created.

The only times where that would matter would be if you did a GetComponent from Awake and the other script hasn’t been initialized yet or if the other script is disabled. Given that the code appears to be polling Input I would say that InventoryGUI is either not on that GameObject or it is disabled (assuming the Input call is being done inside Update).

I was assuming he took our earlier advice and cached the object… looking at the code again it appears he didn’t. Does the order only affect Awake or does it also affect Start? I wasn’t sure.

Start always runs after Awake and within Start you can be sure that the object you’re dealing with has been fully initialized. So a straight GetComponent call in Awake would be contingent on execution order (I think - assuming initialization order is also tied to execution order but it might be as you said and actually be the order of the component attachments in the Editor) but a GetComponent call in Start will always work - again as long as the component is actually attached and enabled.

Would be interested in the actual behavior though. Is Start order identical to Awake order and are those two tied to execution order like Update is?

EDIT: According to the manual the order in which Awake and Start execute is arbitrary but Start will always execute after all of the Awakes. http://docs.unity3d.com/Documentation/Manual/EventFunctions.html

Hey guys, sorry about that, been away.

Ummmm ok Dustin Horne KelsoMRK you 2 have successfully managed to confuse me…

From what i got about your discussion is that the InventoryGUI needs to be placed on a gameobject correct, it is…

Could you explain in baby steps please, i cant understand sugar talk… (Sugar talk is when someone stays on subject explaining stuff that dosnt maker sense to the listener/reader. Its a word i use on my teacher in a friendly way, no offence intended in any way :P)

Sorry… you can ignore some of what we said as we were bouncing ideas back and forth. Basically what you want to do is this… Make sure your InventoryGUI component is attached to the GameObject. Just drag the script to the game object to attach it.

Now, in your other script add this inside your class (not your functions, just inside the class)

private InventoryGUI _inventory;

Then, in the Start function of your script, add this:

void Start()
{
     //Your regular start logic here if you have any

    _inventory = gameObject.GetComponent<InventoryGUI>();

}

And finally your other code would look like this:

if (Input.GetButton ("Inventory")  _inventory != null)
{
     _inventory.ToggleInventoryWindow();
}

EDIT: You’re certainly going to want to be checking if _inventory is null before running this code… that null check is going to be slightly more efficient than the call to Input.GetButton… so you could reduce the order of those. In this case the difference is miniscule and probably nearly imesasurable, but just food for though.

if (_inventory != null  Input.GetButton ("Inventory"))
{
     _inventory.ToggleInventoryWindow();
}

Is it on the same GameObject as the script that has the code you posted?
Is it enabled at the time that code is executed?

Yay it works! Kinda…
All i need to do is set it so that it accquires the inventory (works when i pause game and add the inventory to the variable)
So i shall start on that tomorrow

Now KelsoMRK…

Sorry Kelso, ‘it’? which ‘it’ and what ‘it’? sorry your going to have to treat me as if i was stupid :slight_smile: