Can someone help me with getting a script that'll recognize when I have a certain amount of rocks in my inventory?
Hello, I made a post about this on here about 5-6 months ago, and i'm still having trouble with it. User tanoshimi gave me a snippet of the script that I needed, but since it was incomplete, there wasn't much I could do with it. Anyways, In my game, I need to go around 3 different islands to collect a total of 60 rocks, and after I have all 60 rocks collected, I need to walk into a collider that is a trigger thats on my main island, and when I walk into the collider, it'll cut to an animation and the rocks will be aligned to spell out "S.O.S" But the problem is, I have no idea how to get a script that'll detect when I have 60 rocks in my inventory. I'm pretty sure I can handle the rest, but all I need is a script that'll detect when I have 60 rocks, and then to turn on the collider. If someone can direct me where to go to find out how to get this script, or be nice enough to actually make a script, it'll be greatly appreciated. The inventory script I am using is the free one called "Inventory Master"
Here's the link to the post where "Tanoshimi" gave me an incomplete script.
Or maybe a simpler way would be to make a script where it goes like, when all objects tagged "Stones" are all gone, the collider activates? That seems like it would be easier.
@tanoshimi gave you all that you really needed.
Remember this is a site for people with Unity related questions. It is not a site to $$anonymous$$ch people how to program (there are more than enough of those out there).
tanoshimi is assu$$anonymous$$g that you have some basic knowledge of creating classes in C#. So as he wrote in his reply the snippet
needs to be contained in a proper C# class definition, in which case "playerInventory" becomes a slot exposed in the inspector panel onto which you can drag and drop the player inventory.
What this means is you need to supply this
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // this is needed for Lists
public class $$anonymous$$yClassName : $$anonymous$$onoBehaviour
{
// SNIPPET CODE HERE
}
where // SNIPPET CODE HERE is the snippet supplied by tanoshimi. So basically the resulting class would look something like this
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // this is needed for Lists
public class $$anonymous$$yClassName : $$anonymous$$onoBehaviour
{
[Tooltip("Drag your player's inventory into this slot")]
[SerializeField]
private Inventory playerInventory;
[Tooltip("Set this to the ID of the stone in your inventory system")]
[SerializeField]
private int stoneID;
// Something entered the spot...
void OnTriggerEnter(Collider other)
{
// .. was it the player?
if (other.CompareTag("Player") )
{
// If so, rummage through their inventory
List<Item> items = playerInventory.getItemList();
// and count up how many stones they have
int numStones = 0;
for(int i=0; i<items.Count; i++)
{
if (items[i].itemID == stoneID)
numStones++;
}
// Do they have enough?
if(numStones >= 60)
{
SOSAnimation.Play();
}
}
}
}
But as tanoshimi stated you do need to set up everything in the inspector properly.
Thanks for all the help, I do really appreciate it. Although, even though I clearly have "SOSAnimation" in my game, it says that it doesn't exist in its current context.
Your answer
Follow this Question
Related Questions
Inventory & Co. suggestioni? 2 Answers
Display list as UI or GUI 0 Answers
Concept of inventory system 0 Answers
Inventory System ? 0 Answers
Index was out of range. error 1 Answer