- Home /
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.
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.
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?
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
Follow this Question
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