C# Inventory System calling AddItem function

Ok guys, I think I’m missing something obvious here.

I have a CharacterControl script, and also an Inventory and Item scripts. This is how I have it set up right now:

I have a an onTriggerEnter function on CharacterControl, so when I collide with an Item, it calls:

other.gameObject.SendMessage("LockUnlock");

LockUnlock() is a function I have defined in Item. If the item is unlocked, then it locks it, and if it is locked then I want the item to be “picked up” and Added to the Inventory object in CharacterControl. I know it’s weird that I only want it to be picked up when it is locked.

This is my LockUnlock function in Item:

void LockUnlock () {
		
		if(unlocked)
			unlocked = false;
		else
			player.GetComponent<SphereControl>().AddItemToInventory(gameObject);

}

where SphereControl is my character controller script. This does not work. I don’t really know quite what I am doing wrong.

Can anyone offer any help? I’m sure the solution is pretty simple, but I am just getting start learning Unity.

Where is ‘unlocked’ actually changed?

And, if you are wanting the item to be “picked up” when when an item is unlocked, then lines 3 and 4 of the LockUnlock function make no sense… the code checks to see if the item is unlocked (which is what you say you want) and then immediately changes that status.

I got the same thing when I read it. If it’s unlocked it’s supposed to be able to be picked up - but the script just changes its locked status → meaning it is UMPOSSIBLE!

lol Sorry guys, I know it’s confusing and I should have explained that better. You actually can’t pick up the object until it’s locked instead of unlocked. I’m making an educational game about parallel processing and it’s about mutual exclusion. Sorry for the confusion.

Ok, I just edited the original post because I had it mixed up myself haha.

Soooo… you want the player to pick up the item when they intersect it (walk over it, etc), right?

But you want the player to only pick up “locked” objects, right?

But you have some “unlocked” objects and have a script that when they walk over it - they lock it if it is “unlocked”?

My question is… what is the point of a locked/unlocked state? If you want your code to lock it if the player walks over it - then they backup and walk over it again to pick it up - what’s the point of locking it?

Why not just have the player pick the item up as-is? There’s technically no need for a lock/unlock status if you’re not going to be using it as a restrictive element - since, technically, the way you have it setup and are describing it - it’s not a restrictive element - it’s just restrictive script.

Well, what I plan on doing is something like letting the player click on the object to “lock” it so that no other threads can access it (pick it up) at the same time. Once it’s locked, the player can pick it up, which will end up spawning another object that is also locked. The player then needs to unlock this new object so that other players (threads) can collect it also. As I said, it’s part of a parallel processing concepts thing that I am doing.

My real problem isn’t any of this crazy logic I have going on. It’s my inventory system. As I said I have an inventory script and an item script in C#, along with a SphereControl script. Right now, I am trying to call the SphereControl’s AddItemToInventory from the Item script itself and it doesn’t seem to be working.

Here’s a stupid question. Should I have an actual Inventory object in the game world? Right now, I just have an Inventory script and in the SphereControl script, I declare an Inventory object for the player.