• 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 Chessnutter · Dec 26, 2017 at 04:41 AM · errormovementphysicssyntax-errorsyntax

Error: Cannot cast from source type to destination type

Hello! I am getting an annoying bug and I can't find a way to fix it, Cannot cast from source type to destination type. I think the problem is located somewhere in the following code. If anyone knows how to fix it that would be great, thanks!


private void OnTriggerEnter(Collider LasarCollider) { Collider LaserCollider = gameObject.GetComponent(); if(LaserCollider.transform.parent.tag == "Asteroid") { print("hi"); } }


----> IN A DIFFERENT SCRIPT


if (Input.GetKey(KeyCode.Space)) { GameObject Shoot = Instantiate(LasarBeam, transform.position, transform.rotation).gameObject; }

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 Larry-Dietz · Dec 26, 2017 at 04:52 AM

in your OnTriggerEnter, i am confused as to the purpose of the line Collider LaserCollider = gameObject.GetComponent(); If you are trying to see if you collided with an object with a parent tag of Asteroid you shoudl just dom something like this...

 private void OnTriggerEnter(Collider LasarCollider) {
   if(LasarCollider.transform.parent.tag == "Asteroid")
     print("hi");
 }

Which line is actually throwing the error? I am assuming it is the line I mentioned wasn't needed, as you are asking it to get a component, but not specifying what type of component to get.

Hopefully this helps, -Larry

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 Chessnutter · Dec 26, 2017 at 04:53 AM

The line throwing the error was the 2nd part of the code I posted above. Thanks for your advice, I'll see if it works.

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 Chessnutter · Dec 26, 2017 at 04:55 AM 0
Share

I replaced the code with what you provided, and unfortunately I receive the same error, from the same part of the code. Sorry :(

avatar image Larry-Dietz Chessnutter · Dec 26, 2017 at 03:25 PM 0
Share

So if I am understanding you right, you are getting the error while instantiating the object? Not within the trigger?

Looking at your code for instantiating the object, I don't see anything that stands out. The only thing off the top of my head would be if LasarBeam was null. Is LasarBeam a prefab that you are dragging onto the script in the inspector? If so, have you confirmed that you actually drug something onto it, so it has a value?

Also, just to point it out, instantiate returns a GameObject, so you don't actually need the .gameObject at the end of your instantiate line.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

How to move a Sphere using physics 0 Answers

Player gets stuck into object when it collides with it 0 Answers

Object attached to player doesn't collide 0 Answers

How does Rigidbody.MovePosition works ? What's behind the API? 0 Answers

Smooth Player Ball Rolling 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges