Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheNewGuy · Jun 12, 2011 at 06:40 PM · instantiatespawn

Simple Instantiate/Spawning Example

I'm new to Unity3d I'm trying to learn the basics. Basically I'm trying something simple. When my character collides with a Cube it spawns a rock next to it.

This is my Script.

 var Rock : Transform;
 
 function OnCollisionEnter (collision : Collider) {
     if(collision.gameObject.tag == "Player"){
         Debug.Log("yay");
         Instantiate (Rock);
     }
 }

I have attached this script to the Cube.. I'm getting nothing in the console, nothing is spawning. No errors.

I believe I got this code from one of manual I just change the prefab. Could someone help me out?

Edit I am getting a error

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

um am I missing something?

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
0

Answer by FLASHDENMARK · Jun 12, 2011 at 07:12 PM

The Instantiate function needs 3 variables:

  1. What object you want to Instantiate.

  2. Where it needs to be Instantiated.

  3. The rotation of the Instantiated object.

And it needs to be Collision in the OnCollisionEnter field not Collider.

var Rock : Transform;

 function OnCollisionEnter (collision : Collision)
 {
 if(collision.gameObject.tag == "Player")
     {
     Debug.Log("yay");
     Instantiate (Rock, where I want it, what rotation it needs);
     }
 }
Comment
Add comment · Show 5 · 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 Joshua · Jun 12, 2011 at 07:19 PM 0
Share

Right, and since, as you said, the first variable is a gameObject you should change Transform to GameObject ;)

avatar image Eric5h5 · Jun 12, 2011 at 07:21 PM 1
Share

You don't have to add anything to Instantiate aside from the prefab. "Instantiate (Rock);" is fine.

avatar image Joshua · Jun 12, 2011 at 07:28 PM 0
Share

You're right (http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html) it still needs to be fed an object though, not a transform.

avatar image Eric5h5 · Jun 12, 2011 at 07:57 PM 0
Share

@Joshua: Transform is an object. Read the first example in link you just posted. :)

avatar image Joshua · Jun 12, 2011 at 08:08 PM 0
Share

:o You're right. How stupid of me.

avatar image
0

Answer by Chris D · Jun 12, 2011 at 07:10 PM

OnCollisionEnter passes a collision, not a collider, try changing

 function OnCollisionEnter (collision : Collider) {

to

 function OnCollisionEnter (collision : Collision) {


Also, in your instantiate, you have to provide more details. Check out the overview on it here: http://unity3d.com/support/documentation/ScriptReference/index.Instantiate.html

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 Eric5h5 · Jun 12, 2011 at 07:20 PM 3
Share

You don't have to add anything to Instantiate aside from the prefab. "Instantiate (Rock);" is fine.

avatar image Chris D · Jun 12, 2011 at 08:19 PM 0
Share

good call; I should really read to the bottom of the reference pages :(

avatar image
0

Answer by Eric5h5 · Jun 12, 2011 at 08:01 PM

As the error says, you need to use Collision for the type (and not Collider). By the way, it's better to use CompareTag:

 if (collision.CompareTag ("Player")) {
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Networking: Object is instantiated twice instead of once 1 Answer

Spawn Point Problems 1 Answer

Code based 3d multiple cube instantiation 1 Answer

Instantiate over time? 1 Answer

Instantiated Object Spawning in different position 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