Reference to another script variable not working

Ok I feel like I need to re-explain myself.

Script InteractionScript needs to access the variable ItemName which is under the script WorldItemProperties. WorldItemProperties is located under a gameobject which its name is subject to change because its a Item.

The InteractionScript will access the WorldItemProperties via a Collider with the Item gameobject.

The following scripts…
Line 6 is where the issue occurs with…

col.gameObject.WorldItemProperties.ItemName

InteractionScript (Attached to Player Gameobject)

	public GUIText Interaction;

void OnTriggerStay(Collider col){

		if (col.gameObject.tag == "Item") {
			Interaction.text = "Press E to Pick up " + col.gameObject.WorldItemProperties.ItemName + ".";
			if (Input.GetKeyUp ("e"))
			{
				Item = col.gameObject.WorldItemProperties.ItemName;
				ObtainItem();
			}
		}
       }

Dont refer me to the GetComponant as there isnt a C# example…

OLD EXPLANATION

Did you declare “static” to the variable WorldItemProperties?

“static var WorldItemProperties”

Not sure if I’m right, just trying to help! :slight_smile:

No WorldItemProperties is a script

Relax mate! :smile:

Shouldnt the script WorldItemProperties be referenced in the function?

In the collision detection you reference it with col.gameObject.WorldItemProperties.ItemName but there is no reference in the function to WorldItemProperties.ItemQuantity

Is there any more information needed? PLEASE tell me.

EDIT: Sorry Softwizz, only just noticed your post :slight_smile:

Sorry Softwizz, i dont understand.

The Gameobject the WorldItemProperties is subject to change depending on the item

The Interaction script is attached to the player and must access WorldItemProperties (which is attached to another gameobject to which the gameobject name will change) variable ItemName ItemQunantity

Please note the explanation has been updated…

You cannot say “gameObject.WorldItemProperties” because “WorldItemProperties” is not a property of the GameObject class. You need to use GetComponent to get the WorldItemProperties component and then access the variables through that reference.

void OnTriggerStay(Collider col){
    if (col.gameObject.tag == "Item") {
        WorldItemProperties worldItemPropertiesComponent = col.gameObject.GetComponent<WorldItemProperties>();
        
        Interaction.text = "Press E to Pick up " + worldItemPropertiesComponent.ItemName + ".";

        if (Input.GetKeyUp ("e"))
        {
            Item = worldItemPropertiesComponent.ItemName;

            ObtainItem();
        }
    }
}

You can say things like “gameObject.transform” because the “GameObject” class contains a property called “transform” that returns the associated transform. You can see the other available properties in the documentation here:

Yay! Thank you KyleStaves! and a simple answer to…I should be asshamed of myself…

MonoDevelop likes it, but Unity dosnt…I never came accross these errors before…

error CS0309: The type WorldItemProperties' must be convertible to UnityEngine.Component’ in order to use it as parameter T' in the generic type or method UnityEngine.GameObject.GetComponent()’

error CS0120: An object reference is required to access non-static member `WorldItemProperties.ItemQuantity’

public GUIText Interaction;
	public string Item = "";


		// Use this for initialization
	void Start () {

		Interaction.text = "";
	}

	void OnTriggerStay(Collider col){

		WorldItemProperties WorldItemProperties = col.gameObject.GetComponent<WorldItemProperties>();
                //GetComponant is here to get access to other functions in the script, I.e ObtainItem


		Debug.Log("Player is in collider");
		
		if (col.gameObject.tag == "Item") {

			Interaction.text = "Press E to Pick up " + WorldItemProperties.ItemName + ".";
				Debug.Log ("This is an item!");
			if (Input.GetKeyUp ("e"))
			{
				Item = WorldItemProperties.ItemName;
				ObtainItem();
			}

Anyway I have a headache from this and am tierd so im of to bed and will resume this in the morning.
Thanks Kyle!

GameObject.GetComponent() expects to find a type that is of type Component or extends it. Is WorldItemProperties a monobehavior or an interface? If it’s an interface, you can try

col.gameObject.GetComponent(typeof(WorldItemProperties)) as WorldItemProperties;

I think that one is because you’re naming your variable the same thing as the class name (on line 13). While that technically works in some cases, it’s generally not a good idea.

He is using GetComponent() instead of GetComponent(), so your cast is not necessary.

GetComponent<> does not work with interfaces, however, which is why Lawros was suggestion the manual cast (which does work with interfaces).

Error CS0309 is thrown because WorldItemProperties is not a UnityEngine.Component. Without seeing the full class we can only guess why, but based on Ethanbf3’s level of experience I’d say it being an interface is probably pretty doubtful. More likely he just forgot to extend from MonoBehaviour for his class declaration.

Ethan, mind showing us the full class? You probably just need to change

public class WorldItemProperties

to

public class WorldItemProperties : MonoBehaviour