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 MohsenEbrahimi · Sep 13, 2011 at 07:04 AM · c#instantiatecs1502cs1503typecast

Problem with Instantiate() Function in C#

Hi all. I want to instantiate a bulb when a collision enter. but OnTriggerEnter() works correctly but when I add a piece of code that Instantiate a Object the compiler show some errors:

(Filename: Assets/MyScripts/Animals.cs Line: 21)

Assets/MyScripts/Animals.cs(21,25): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, > UnityEngine.Quaternion)' has some invalid arguments > > (Filename: Assets/MyScripts/Animals.cs Line: 21) > > Assets/MyScripts/Animals.cs(21,25): error CS1503: Argument #1' cannot convert object' expression to type UnityEngine.Object'

(Filename: Assets/MyScripts/Animals.cs Line: 21)


the code is: bullet sp = Instantiate( bullet, transform.position, transform.rotation); sp.transform.localScale.Scale(new Vector3(radius , radius , radius));

I have read the Script reference and Documentation but i confused why this code has type conversion error. please Help me. thanks for your help.

Comment
Add comment · Show 1
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 CHPedersen · Sep 13, 2011 at 07:11 AM 0
Share

As the error states, the problem lies with the 1st argument, which is the variable "bullet". That variable is not of type "UnityEngine.Object" which means that you've likely declared it to be something else. The usual approach here is to declare a public variable of type Transform in the script, and then drag a prefab onto that variable in the editor. Transform is of type UnityEngine.Object. Use that.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GuyTidhar · Sep 13, 2011 at 07:18 AM

You need to give the Instantiate function a Unity object (usually a prefab) from which unity will create the new instance. In the following case:

 bullet sp = Instantiate( bullet, transform.position, transform.rotation);

You have actually tried to create an instance using the type definition ("bullet"). You can not do that. You could however do something like the following:

 // In the edito, create a prefab of type bullet and drag it to the GameObject on which you have attached this script
 public bullet referenceToBulletPrefab;

 void OnTriggerEnter(Collider other)
 {
    // Put here the rest of the code
 
    // Create a new instance of a bullet using the prefab: referenceToBulletPrefab
    bullet sp = Instantiate( referenceToBulletPrefab, transform.position, transform.rotation);

    // Put here the rest of the code
 }
Comment
Add comment · Show 4 · 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 MohsenEbrahimi · Sep 13, 2011 at 11:35 AM 0
Share

I try it but it doesn't work at all. I have create bullet prefab define a public Transform variable and attach bullet prefab to Transform variable but the errors is the same.

avatar image GuyTidhar · Sep 13, 2011 at 11:49 AM 0
Share

Is the prefab root a GameObject on which your script "bullet" is attached?

avatar image MohsenEbrahimi · Sep 13, 2011 at 11:59 AM 0
Share

I change the language from C# to JavaScript. Your $$anonymous$$ethods works Correctly. thank you. :)

avatar image GuyTidhar · Sep 13, 2011 at 12:09 PM 0
Share

Cool man. Glad to had helped.

Note that it is accustomed to mark an answer as correct (if it is) + give a thumbs up.

avatar image
0

Answer by MohsenEbrahimi · Sep 13, 2011 at 12:06 PM

Thanx for your answer but the problem when occured the Instantiate() return UnityEngine.Object and isn't a prefab such as bullet!! I'm hanging on it!!!

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 SisterKy · Sep 13, 2011 at 11:03 AM 1
Share

please use 'add a comment' ins$$anonymous$$d of an-answer-to-your-question for such annotations.

avatar image
0

Answer by Owen-Reynolds · Sep 13, 2011 at 02:50 PM

You can still use C#, if you want. Most people in javascript use untyped variables, which works great when you don't know the type. You get to say:

 public bulletPrefab : Transform;

 newbullet = Instantiate( bulletPrefab ..... );
 newbullet.regidbody.velocity = ....

We don't know what type the instantiated bullet is, but whatever it is, newbullet is declared as one of those. In C#, you just have to know Instantiate returns a gameObject, and how to convert that to a Transform (this is in the Instantiate ex in the Unity manual, if you switch it to C#):

 Transform newbullet = Instantiate(bulletPrefab .... ) as Transform;
 newbullet.rigidbody....   // can now use it like a normal Transform
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

Targetting script errors 1 Answer

Distribute terrain in zones 3 Answers

error with GUI in c sharp 2 Answers

CS1502 and CS1503 Error 0 Answers

Multiple Cars not working 1 Answer


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