Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by beefwellington2 · Oct 18, 2012 at 12:55 AM · gameobject

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hvilela · Oct 18, 2012 at 12:56 AM

You're right. "Instantiate" is the right way to make an object appear.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beefwellington2 · Oct 18, 2012 at 01:16 AM 0
Share

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?

avatar image hvilela · Oct 18, 2012 at 01:21 AM 0
Share

Yes. Look at the Instantiate documentation, it's all there.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beefwellington2 · Oct 18, 2012 at 05:39 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Simple code is not recognizing tag anymore. 0 Answers

how to make my gameobjects disable and enable? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges