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 SS · Nov 10, 2010 at 06:52 AM · collisiontransformclone

Projectile movement works on one, not on the other

Excuse me if this has been asked, but I've been searching for a long time, and can't seem to find the problem (I'm quite inexperienced as well). If the question has already been answered, feel free to link it.

I have the player creating and moving a projectile (it works exactly how I want it), but the same exact script won't work on the enemy I've created. The enemy and player are exactly the same (to my knowledge), except for the "third person controller" script, and the "character controller".

The enemy will create the projectile, but it won't move. It stays in the middle of the enemy, where it's created.

Player script

var projectile : Transform; var phealth : int = 3;

function FixedUpdate () { if (Input.GetButtonDown ("Fire1")) { var cloneprojectile : GameObject = Instantiate (projectile, transform.position, transform.rotation); cloneprojectile.transform.Translate(0,0,.8);
Destroy (cloneprojectile, Time.deltaTime * .001); } }

Enemy script

var projectile : Transform; var ehealth : int = 3;

function FixedUpdate () { if (Input.GetButtonDown ("Fire2")) { var eprojectile : GameObject = Instantiate (projectile, transform.position, transform.rotation); eprojectile.transform.Translate(0,0,.8);
Destroy (eprojectile, Time.deltaTime * .001);
} }

The projectile is a trigger, with a kinematic rigidbody to do damage.

I have the error "InvalidCastException: Cannot cast from source type to destination type." on the line

var eprojectile : GameObject = Instantiate (projectile, transform.position, 
                                            transform.rotation);

It's just tough because the script works in one case, but not the other.

This is probably really easy for you to answer haha.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mike 3 · Nov 10, 2010 at 08:09 PM

This is your problem:

var projectile : Transform;
var eprojectile : GameObject = Instantiate(projectile, ...

You're putting a Transform object into Instantiate, which will return a Transform object, and then you're trying to cast it to GameObject. The editor then throws an exception, halting the execution of the script

Change it to

var eprojectile : Transform = Instantiate(projectile, ...

and it should then continue onto the next lines of code.

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 SS · Nov 11, 2010 at 01:23 AM 0
Share

Thanks $$anonymous$$ike! It worked, kind of. I had to play around with it to make it fit.

As a transform, when I try to delete it, the script only wants to delete the transform. It won't work because the other components (mesh renderer, etc.) of eprojectile depend on the transform. So I had to make the destroy

Destroy (eprojectile.gameObject,...001);

The thing I don't understand, is that while transform works for the var types on the enemy script, only gameObject works on the player script. If I try to use transform as vars, it gives me the InvalidCastException error. Any clue why that is?

avatar image Mike 3 · Nov 11, 2010 at 04:46 AM 0
Share

It's probably because of how unity assigns objects to the scripts at runtime. If you change the type of a public variable without changing the name, unity doesn't reevaluate whether it needs to change what object is referenced and actually tries to assign the wrong object at runtime, so what you need to do is set it to some other object, then back to the object you want to instantiate again, and it should fix it

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

No one has followed this question yet.

Related Questions

Transform.Position Collision Issue 0 Answers

SetParent keeps cloning my gameObject? 0 Answers

Having trouble understanding parenting with componants 0 Answers

Stop moving when colliding. 2D 1 Answer

OnCollisionEnter not called when colliding with clone 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