- Home /
Need help with a trigger problem?
I need help with the following code it will not allow me to run it i am trying to make it so that when the game object collides with the trigger it will push the game object ahead a certain amount of meters.
using UnityEngine; using System.Collections;
public class pushcharacter : MonoBehaviour {
private Vector3 speed = new Vector3(3, 0, 0);
void FixedUpdate() {
rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
}
}
public class Transform explosion;
public class Transform trnsfrm;
void OnTriggerEnter (Collider obj)
{
Instantiate (explosion, trnsfrm.position, Quaternion.identity);
}
Answer by Michael Covert · Oct 17, 2012 at 09:05 PM
When you say it won't let you run it, you mean it won't compile? Normally, telling us the errors you're seeing more specifically can help pinpoint the problem.
In this case, I think your problem is the declaration of your public variables explosion and trnsfrm, they shouldn't have class before them - i.e,
public Transform explosion;
public Transform trnsfrm;
Does that solve the problem for you?
Still the problem heres the code:
public class pushcharacter : $$anonymous$$onoBehaviour public class Transform explosion; public class Transform trnsfrm; { private Vector3 speed = new Vector3(3, 0, 0); void FixedUpdate() { rigidbody.$$anonymous$$ovePosition(rigidbody.position + speed * Time.deltaTime); } void OnTriggerEnter (Collider obj) { Instantiate (explosion, trnsfrm.position, Quaternion.identity); } }
---------The error it says is under the public part of public class Transform, it says "type expected".
Thanks so far though!
You should only use the class keyword when declaring a class. It looks like you're trying to declare instances of a class, in which case you shouldn't use the class keyword. It's expecting you to provide a type ins$$anonymous$$d - which you do, but after the erroneous class keyword, so they compiler's already "thrown-up" so to speak. Removing the class keyword from your two Transform variable declarations should solve the problem.
Correct, this will solve the compiler error, but you've still got some work to get the desired behavior... The only thing that is initiated by your trigger is to instantiate your explosion.. But your movement code is in FixedUpdate, which is not going to wait for the trigger, it will be moving the whole time..