- Home /
I have my items in inventory, now if I could only use them...
EDIT:
It's dawned on me that I've really been overcomplicating this. It also dawns on me that because of my lack of experience, I'm having a dickens of a time figuring out what to do next. The long and the short of my issue: I have inventory, I'd like to use it. For instance, a health potion. If I have a health potion, and I drink it, I'd like it to add 15 hp. The major problem I found was that my PlayerHealth script is in C#, while my inventory script is in js. As a noob I've just been finding tutorials, and writing (or editing) scripts based on what I learned that particular day, never thinking there would be an issue what language I coded them in. (go ahead, laugh) And so, for the last couple of days I've been trying to pull code from the PlayerHealth C# script to make the +hp function work, with no success. Today I found an online converter, which I didn't expect to solve all of my issues, and converted the js Inventory script to C#, thinking that might be a start in helping to simplify this. Here's the converted script:
// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
// Do test the code! You usually need to change a few small bits.
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour {
//inventory
static int[] inventoryArray = [1,2,0,0,0];
GameObject inventoryText;
GameObject PauseCamera;
void Update (){
inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n" + "Mana Potion " + "[" + inventoryArray[1] + "]";
if(Input.GetKeyDown("g")) {
if(inventoryArray[0] > 0) {
healthPotion();
}
}
//inventoryArray[0]++;
//inventoryArray[1] += 2;
}
void healthPotion (){
PlayerHealth.curHealth += 15;
inventoryArray[0] -=1;
}
}
I've gotten a compile error at 10:31 that states "Assets/Inv2.cs(10,31): error CS1519: Unexpected symbol `' in > class, struct, or interface member > declaration" And a parsing error at line 41 (it's actually an empty line following the final bracket. Added a bracket, and the compiler didn't like that, so left it out for now. Definitely expected there to be work after the conversion, just am in need of a nudge in the right direction. Thanks, and God bless. Original thread is below.(since that script still exists in the event that this wasn't the route I should take) I did search, plus fiddled with several things before resorting to asking this. I don't think I'm far from the answer, but maybe I'm wrong. Here's what's going on: Am working on learning a basic inventory script. Have gotten the inventory attached to my pause menu, am able to add items to it, and to have a GuiText display said items. Now comes the part where I actually use them. Let's take, for instance, a health potion. The instructions I'm following in a tutorial are really straight forward, but they're assuming that I'm scripting from js to js. In other word, the PlayerHealth script (which the health potion regenerates, PlayerHealth.curHealth) is in js, as is the pickupitem script. Unfortunately that's not my case, my PlayerHealth is in C#. Now, I've found some resources online [namely here, and came up with the following based on it:
//inventory
static var inventoryArray : int[] = [1,2,0,0,0];
var inventoryText : GameObject;
function Update () {
inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n" + "Mana Potion " + "[" + inventoryArray[1] + "]";
if(Input.GetKeyDown("g")) {
if(inventoryArray[0] > 0) {
healthPotion();
}
}
//inventoryArray[0]++;
//inventoryArray[1] += 2;
}
function healthPotion () {
//create a variable to access the C# script
}
private var PlayerHealth : PlayerHealth;
function Awake()
{
//Get the CSharp Script
csScript = this.GetComponent("PlayerHealth"); //Don't forget to place the 'CSharp1' file inside the 'Standard Assets' folder
}
//...
PlayerHealth.curHealth += 15;
inventoryArray[0] -=1;
which from the appearance I'd say is missing something, but I'm just not sure what. In addition (or possibly because of this) I'm getting some compiler errors, specifically:
"UnassignedReferenceException: The variable inventoryText of 'Inventory' has not been assigned. You probably need to assign the inventoryText variable of the Inventory script in the inspector. Inventory.Update () (at Assets/scripts/Inventory.js:8)" (also 39)
Sorry for REALLY being a noob here, but this confuses me, as I've assigned the inventory text to the Inventory script as evidenced in the lower right corner of this photo:
with this being the (current) intended result, which was successful:
So I've got errors showing at lines 8 and 39 saying that the text isn't assigned, but to my untrained eyes it appears to be, and an inventory that I can add to, but not subtract from, that looks to be caused by me not quite fully pulling my PlayerHealth curHealth variable from the C# script into the js script. Am I correct to say those are my issues, and is there anyone who might be able to suggest my next step? Thanks, and God bless. =)
When you run the scene and look at the inspector, is Inventory text still assigned?
The screenshot you have posted shows it when the game isn't running. Could you post another one with the game running and showing the inspector?
Also, have you ever thought of converting your all your code to just one language? I never understood why people choose to have scripts using different ones, it just over complicates things.
Hey $$anonymous$$issing, thanks for responding. Just ran the game again, and yep, it still shows that text as being still assigned in the inspector. If you'd like me to post a screenshot, I can, but there's no change.
As to thinking of converting all my code to one language, I hadn't given any thought to it, until last night, that is. Am learning as I go (it would take me 1000 years to figure all this out if I tried to learn it "school style" first) and never had anticipated this type of thing being a complication.
If you click on the error, it should point you to the variable that is not assigned. This seems like a case where you have multiple instances of inventory (on different GameObjects), but have only assigned one of those.
Thanks Jamora, definitely seems to have pointed me in the right direction. I see that I had the Inventory script added in two locations. Deleting one removed two of the three errors. I also notice now that in the Hierarchy panel, the one item that still has Inventory attached (and specifically the child item GuiText that is the inventory text) is greyed out, yet still attached as Inventory text.) This is the only remaining error:
NullReferenceException: Object reference not set to an instance of an object Inventory.$$anonymous$$ain () (at Assets/scripts/Inventory.js:39)
Now, assu$$anonymous$$g that's referring to line 39 of the inventory script, here is that line:
PlayerHealth.curHealth += 15;
which is my attempt to use it from another script. In context:
function healthPotion () {
//create a variable to access the C# script
}
private var PlayerHealth : PlayerHealth;
function Awake()
{
//Get the CSharp Script
csScript = this.GetComponent("PlayerHealth"); //Don't forget to place the 'CSharp1' file inside the 'Standard Assets' folder
}
//...
PlayerHealth.curHealth += 15;
inventoryArray[0] -=1;
It totally makes sense that this is where I've screwed up, as this is the place I'm attempting to pull from a js code to C#.
Here's my resource for null references, why you get them and how to try to fix them.
This particular null reference error is because you never assign to PlayerHealth.
This answer tells how to access scripts of other languages in Unity.
Answer by Conect11 · Sep 21, 2013 at 02:10 AM
well, sometimes we have to go back to Occam's Razor, which states that the best solution is usually the simplest one, and the simplest solution in this case was just to go back to all js. Thank you everyone for your help, I don't intend to abandon the lessons learned from this.
Your answer
Follow this Question
Related Questions
C# scripting and Javascript in the same project? 3 Answers
Cant get C# to talk to JavaScript 3 Answers
saving inventory to database 3 Answers
Inventory Drop Function Problem 1 Answer
How to make a simple inventory?!? Beginner coder JS 1 Answer