• 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 DerpingBuddha · Sep 09, 2013 at 02:43 AM · instantiation

Insantiation problem

Working on trying to get a character model i exported from 3ds max instaniated when a game starts. I been following the PhotonCloud tutorial "Marco Polo", and I can't seem to get it to quite work. Every time this code is called or used (once the game/scene practically starts:

 void OnJoinedRoom(){
         GameObject monster = PhotonNetwork.Instantiate("lawlzy",Vector3.zero, Quaternion.identity,0);
     }


I get this error "Cannot cast from source type to destination type"

Please help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SparkGames · Sep 09, 2013 at 03:08 AM

Doesn't your instantiate have a lot of parameters?

anyways try adding a cast

(GameObject)PhotonNetwork.Instantiate("lawlzy",Vector3.zero, Quaternion.identity,0);

or more info on this:

http://answers.unity3d.com/questions/38271/cannot-cast-from-source-type-to-destination-type-i.html

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 DerpingBuddha · Sep 09, 2013 at 03:46 AM 0
Share

So I tried the casting method like this:

  void OnJoinedRoom()
     {
         GameObject monster = (GameObject) PhotonNetwork.Instantiate("lawlzy",Vector3.zero, Quaternion.identity,0);
         }

and for a split second, it said "PeerCreated" in the debug area, but nothing spawned and the same error showed up...

avatar image perchik · Sep 09, 2013 at 03:56 AM 0
Share

I should ask, is the error message actually referring to the line of code you provided? Perhaps something happens when "lawlzy" is instantiated that is throwing an error?

avatar image DerpingBuddha · Sep 09, 2013 at 04:00 AM 0
Share

Yes, the error is linked directly to the line of code provided.

To create a prefab, you right click the asset, select prefab, and then move said lawlzy.prefab to a folder called "Resources" in the root project folder, correct?

avatar image perchik · Sep 09, 2013 at 04:27 AM 0
Share

well yes, but I've found your problem

avatar image DerpingBuddha · Sep 09, 2013 at 04:30 AM 0
Share

Code error or seems more likely parameter error?

avatar image
0

Answer by perchik · Sep 09, 2013 at 04:30 AM

When you call instantiate, you have to pass the actual object you want to load to the script. Instead of the object, you're passing the name of it.

You need to use Resources.Load("lawlzy") to load the actual object from the resources directory:

   GameObject monster = (GameObject) PhotonNetwork.Instantiate(Resources.Load("lawlzy"), Vector3.zero, Quaternion.identity,0);
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 DerpingBuddha · Sep 09, 2013 at 04:40 AM 0
Share

Ok so after testing my old code, with your added code:

 GameObject monster = (GameObject) PhotonNetwork.Instantiate(Resources.Load ("lawlzy"), Vector3.zero,Quaternion.identity,0);


I get these errors:

  1. InvalidCastException: Cannot cast from source type to destination type.

  2. Assets/Scripts/Network.cs(24,65): error CS1502: The best overloaded method match for PhotonNetwork.Instantiate(string, UnityEngine.Vector3, UnityEngine.Quaternion, int)' has some invalid arguments 3. Assets/Scripts/Network.cs(24,65): error CS1503: Argument #1' cannot convert UnityEngine.Object' expression to type string'

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Awake() not called on script referenced directly from Project window 1 Answer

Character Collision Issue 1 Answer

Al Robot Animation Problem 0 Answers

Access components in ALL children 1 Answer

Water4 reveals all underwater objects when Global Fog is applied to the camera 0 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