- Home /
Picking up Artifacts and then Using them
Ok, this is going to be REALLY dificult to explaine so I hope you will bear with me on this.
What I'm after is, I need my FPS player to be able to find "Alien Artifacts" that will be hidden or just lying about certain "Game Levels", Like say he finds one "Alien Artifact" in level 1. I then want that "Alien Artifact" to appear on an inventory type screen that I would like to have full screen, designed in Photoshop that will have place holders for each "Alien Artifact" (4 in total)
I'm gonna go out on a limb here and guess that my inventory screen for this will need to be a new "Scene" (Similar to lets say a "Start Screen") that can be accessed anytime from a keypress, maybe "I" for inventory or whatever you think is best. THINK Metroid Prime kinda idea for inventory screen.
As my player picks up (Finds) a missing "Alien Artifact" it gets added into the assigned inventory space on the screen. Also I would like that if each item on the inventory screen is clicked there would be information about it displayed.
Later on in my game when my FPS player has collected all of these "Alien Artifacts" they will enable him to control something or gain access to something important.
Now How the Frak can I do all that lol ;)
I also updated my Blog with a picture of my Full Screen GUI texture Background and info about what I'm trying to do with it. http://zentaiguysblog.blogspot.com/
Edit:
I've changed my code to this, it's not complete I know. I need help to complete it and get it working. I've made a transparent PNG file to represent the first "Alien Artifact" that I would like to appear in the top triangular spot placeholder when my FPS player has picked up the artifact. Here is what the code looks like now (Incomplete though):
var i = 0; var alienArtifacts = 0;
onGui(); { if(i == 1) //put the code that makes the item holder here //you might want to add code for which ones you have got
}
update() { if (/one of the artifacts was picked up/) { alienArtifact ==1; //other stuff like what artifact was picked up } }
function Update () { transform.position.x = 0; transform.position.y = 0;
guiTexture.pixelInset.x = 0;
guiTexture.pixelInset.x = 0;
guiTexture.pixelInset.width = Screen.width;
guiTexture.pixelInset.height = Screen.height;
}
Answer by zmar0519 · Apr 20, 2011 at 12:44 AM
this is my inventory code(you may need to modify the position values to accomidate for your textures):
Inventory.js:
var slotSize : float; var slotSpacing : float; var toggleKeys : KeyCode[]; var buttonPos : Rect; var backgroundTexture : Texture2D; var equipItemPos : Transform; var Skin : GUISkin;
private var scrollPosition : Vector2 = Vector2.zero; private var objectsInBag : GameObject[]; private var texturesForBag : Texture2D[]; private var nextObjectInt : int = 0; private var currentItem : GameObject; private var InvWindowRect : Rect; private var showBag : boolean = false;
function Awake() { var i : int; i = 10*4; objectsInBag = new GameObject[i]; texturesForBag = new Texture2D[i]; InvWindowRect = Rect(10,20,(slotSize+slotSpacing)*4+50, 350); }
function AddObject(j : GameObject) { objectsInBag[nextObjectInt] = j; texturesForBag[nextObjectInt] = j.GetComponent(Item).icon; nextObjectInt++; }
function EquipItem(i : GameObject) { currentItem = i; for(var j = 0; j < objectsInBag.length; j++) { if(objectsInBag[j] != null) { objectsInBag[j].transform.position = this.gameObject.transform.position; objectsInBag[j].transform.rotation = this.gameObject.transform.rotation; objectsInBag[j].SetActiveRecursively(false); } } i.transform.position = equipItemPos.position; i.transform.rotation = equipItemPos.rotation; i.SetActiveRecursively(true); }
function OnGUI() { GUI.skin = Skin; if(GUI.Button(buttonPos, "Bag")) { ToggleBag(); } if(showBag) InvWindowRect = GUI.Window (0, InvWindowRect, InventoryWindow, "Inventory"); }
function ToggleBag() { if(showBag) showBag = false; else showBag = true; }
function InventoryWindow(windowID : int) { if(currentItem != null) GUI.Label(Rect(41, 292, 250, 30), "Current Item: "+currentItem.GetComponent(Item).Name);
scrollPosition = GUI.BeginScrollView (Rect (15, 90,(slotSize+slotSpacing)*4+17, 200), scrollPosition, Rect (10, 10, (slotSize+slotSpacing)*4, (slotSize+slotSpacing)*10));
for(i = 0; i < 10; i++)
{
for(j = 0; j < 4; j++)
{
if(GUI.Button(Rect(10+(j*(slotSize+slotSpacing)), 10+(i*(slotSize+slotSpacing)), slotSize, slotSize), texturesForBag[(j*10)+i]))
{
EquipItem(objectsInBag[(j*10)+i]);
}
}
}
GUI.EndScrollView ();
}
function Update() { if(currentItem.GetComponent(Item)) Destroy(currentItem.GetComponent(Equippable)); for(i = 0; i < toggleKeys.length; i++) {
if(Input.GetKeyUp(toggleKeys[i])) { ToggleBag(); } } }
Item.js(its short, but bear with my):
var icon : Texture2D;
var Name : String;
Equippable.js:
var selectedColor : Color; var isTriggeredByCollision : boolean; var useTag : boolean; var collisionTag : String; private var oldColor : Color;
function Awake () { oldColor = this.gameObject.renderer.material.color; } function OnMouseOver() { this.gameObject.renderer.material.color = selectedColor; if(Input.GetMouseButtonUp(0)) { transform.parent = FindObjectOfType(InventoryGUI).gameObject.transform; this.gameObject.active = false; transform.position = Vector3(transform.parent.GetComponent(InventoryGUI).equipItemPos.position.x, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.y-10, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.z); FindObjectOfType(InventoryGUI).AddObject(this.gameObject); } } function OnCollisionEnter(col : Collision) { if(isTriggeredByCollision) { if(useTag) { if(col.gameObject.tag == collisionTag) { transform.parent = FindObjectOfType(InventoryGUI).gameObject.transform; this.gameObject.active = false; transform.position = Vector3(transform.parent.GetComponent(InventoryGUI).equipItemPos.position.x, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.y-10, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.z); FindObjectOfType(InventoryGUI).AddObject(this.gameObject); } } } else { transform.parent = FindObjectOfType(InventoryGUI).gameObject.transform; this.gameObject.active = false; transform.position = Vector3(transform.parent.GetComponent(InventoryGUI).equipItemPos.position.x, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.y-10, transform.parent.GetComponent(InventoryGUI).equipItemPos.position.z); FindObjectOfType(InventoryGUI).AddObject(this.gameObject); } } function OnMouseExit() { this.gameObject.renderer.material.color = oldColor; } function OnEnable() { this.gameObject.renderer.material.color = oldColor; } function OnDisable() { this.gameObject.renderer.material.color = oldColor; }
I had to choose an answer before time ran out, $$anonymous$$yin was making a special one but then they stopped for some reason, so I chose yours as it's the only complete answere given. I'm hoping that it will work with my GUI full screen image that I have set up. I'll have to give it a go this weekend and see if this works with what I've got in $$anonymous$$d ;) Thanks Joe :D
Not to worry $$anonymous$$yin. I think your code is probably more customized to what I need. The onbe above I have no idea how to implement it although it appears complete. Your I think it probably more suited to what I'm trying to do... I think... this GUI stuff I'm REALLY not good at or all that familiar with so I REALLY need to be baby stepped through it... instruction on what goes where... that sort of thing ;)
O$$anonymous$$ $$anonymous$$yin, lets see if we can knock this thing out of the park. You've seen my Big full screen inventory for my "Alien Artifacts" right? What do I do with that first off?
This example is what I have been looking for but how do you implement this script, what goes where???
Answer by Justin Warner · Mar 28, 2011 at 11:38 PM
You're doing a heck of a lot better than most "first timers" here... You kind of broke it up in to pieces...
However, the idea for the inventory to be a new scene, in my opinion, is a horrible idea...
I think a good question to ask/get clarification on (I don't know the answer), is how to allow a scripts info to continue on to new scene loads... (Refreshed, I know of playerprefs, so maybe do look in to that, www.google.com can help lots!).
Now, you'd want to set up multiple scenes as levels, and use the playerprefs to go through them...
And as Bobadebob said, you should use GUI, especially for the whole inventory...
As for picking up items, look in to raycasting... That should allow you to do well, and look in to tag system to check if it's a pick-up-able... Then give each item a new name, and with that name, allow it to activate a boolean as to whether or not the player has it....
I think this answers most of your questions... If you have more, post more questions, or if I missed something, comment back...
Thanks Guys, I edited my question with what I have updated it with.
Answer by Jesse Anders · Mar 28, 2011 at 11:35 PM
This won't address all the questions in your post, but the inventory screen doesn't need to be a separate scene. Depending on how you want it to work, it can instead be an overlay or otherwise part of the UI for the game, rendered using the built-in GUI system or implemented using other means.
As for making the data persistent, the most straightforward solution would probably be to store which/how many artifacts have been collected using PlayerPrefs (which will allow the data to persist between runs of the application as well as from scene to scene).
Answer by burgunfaust · Apr 14, 2011 at 03:14 AM
I might just use a separate camera rather than a new scene. Keeping things set up properly across scenes can be a pain, so I would try to avoid that option.
Answer by DaveA · Apr 15, 2011 at 12:23 AM
It doesn't look like anyone's mentioned these ideas for persistence: put your inventory into an object which doesn't get destroyed, see this: http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html and look into 'singletons' like maybe this: http://www.unifycommunity.com/wiki/index.php?title=AManagerClass
Your answer
![](https://koobas.hobune.stream/wayback/20220613115410im_/https://answers.unity.com/themes/thub/images/avi.jpg)