- Home /
How to make an object appear after collecting items?
what I want to do is have the player collect three items, then a key will appear. I think it has something to do with instantiate but I'm not sure. here's my code so far
var jewels : int=0;
function OnTriggerEnter( other : Collider ) { if (other.tag == "gem") { jewels += 1; Destroy(other.gameObject); }
}
what would be a really simple way of having this fourth object appear?
Answer by hvilela · Oct 18, 2012 at 12:56 AM
You're right. "Instantiate" is the right way to make an object appear.
but what would the script look like?
if (jewels == 3) { Instantiate (game.Object); }
something like that? do i add it to the gameObject i want to instantiate or put it on the end of the script i mentioned above?
Yes. Look at the Instantiate documentation, it's all there.
Answer by fafase · Oct 18, 2012 at 06:29 AM
var jewels : int=0;
var key:GameObject;
function OnTriggerEnter( other : Collider ) {
if (other.tag == "gem") {
if(++jewels >= 3){
Destroy(other.gameObject);
Instantiate(key,Vector3(200,200,200),Quaternion.identity);
}
}
This is attached to the player. The collision you got it already. Then it increases and checks if you have three gems. It is not necessary but recommended to <= or >= because if for some uncontrolled reasons (you are never free from bugs) your gem counter goes beyond 3 you will never get your key. Though pretty unlikely but who knows... The other object is destroyed and a new one is instantiate.
Instantiate (object,position,rotation);
Drag and drop the key prefab in to the key slot in the Inspector. Instantiate will create that object at the position given in parameter 2. In my case, at 200,200,200. Quaternion.identity is a "fancy" name for no rotation. Without too much details, a Quaternion is a rotation system in 4D.
Answer by CaioRosisca · Oct 18, 2012 at 03:41 AM
function OnTriggerEnter( other : Collider ) { if (other.tag == "gem") { jewels += 1; Destroy(other.gameObject); Instantiate (Key, Vector3(1,1,1), Quaternion.identity); } }
All you need is a frepab named Key
thanks for the response! i tried it out though but it didn't work. i have a prefab called "$$anonymous$$ey" but I get an error that says "unknown identifier: '$$anonymous$$ey""
Your answer
Follow this Question
Related Questions
Component or Independent class 1 Answer
How to disable gameobjects in code? 4 Answers
How do I make an AI that tries to find the player? 1 Answer