News Forums RAIN General Discussion and Troubleshooting Instantiating rigidbody, wont let me alter the velocity

This topic contains 3 replies, has 2 voices, and was last updated by  prime 2 years, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33425

    cosmo22
    Participant

    hey i have a enemy with ai and im trying to get it to fire fireballs at the player, i have a script that handles the trajectory but when i instantiate my ai through rain it wont let me change the velocity, it just gives me a null reference exception, i dont think it is the trajecory code as i have tried to change its velocity with just a vector3.up * 5 but it still gives me the null referenece exception, here is the code for instantiating the fireballs:

    public override void Start(AI ai)
    {
    fireBallAttack = GameObject.FindObjectOfType<FireBallAttack> ();
    spawnPoint = ai.WorkingMemory.GetItem<GameObject> (“spawnPoint”).transform;
    bullet = ai.WorkingMemory.GetItem<GameObject> (“bullet”);
    target = GameObject.FindGameObjectWithTag (“Player”).transform;
    base.Start(ai);
    }

    public override ActionResult Execute(AI ai)
    {

    Rigidbody instantiatedBullet = AIRig.Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity) as Rigidbody;
    instantiatedBullet.rigidbody.velocity = fireBallAttack.MoveTowards(ai.Body.transform.position, target.position,10); <—- this is the like it gives me the null reference exception on

    Physics.IgnoreCollision(instantiatedBullet.collider, ai.Body.collider);
    Debug.Log(instantiatedBullet.velocity);

    return ActionResult.SUCCESS;

    }
    THE ERROR:
    NullReferenceException: Object reference not set to an instance of an object
    FireBall.Execute (RAIN.Core.AI ai) (at Assets/AI/Actions/FireBall.cs:33)

    • This topic was modified 2 years, 5 months ago by  cosmo22.
    #33427

    prime
    Keymaster

    I’m sure instantiatedBullet is null. Shouldn’t that be

    GameObject instantiatedBullet = GameObject.Instantiate(bullet, spawnPoint.transform.position, Quaternion.identity) as GameObject;
    #33435

    cosmo22
    Participant

    nah it wasnt null, it was instantiating the bullet, but i fixed it by doing the same as you did there and doing instantiatedBullet.velocity….. thanks for the help

    #33436

    prime
    Keymaster

    Interesting. Maybe this is a Unity trick I’m unaware of. In Start you are retrieving bullet as a GameObject. When you are instantiating you are casting to RigidBody. Does Unity override that operator to give you an attached component (a RigidBody in this case)?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.