If there’s a rigidbody on your “fireBallObj” you should be able to use:
bulletfire.AddForce(transform.forward * 1000);
This is possible because you’re storing a reference to the newly instantiated “fireBallObj”'s rigidbody component into the “bulletFire” rigidbody variable you’ve created the line before. The error you’re getting suggests that the “fireBallObj” prefab doesn’t have a rigidbody component attached at the root level.
A litte confused of what you said, since all this vocab is new to me. Yet on my FireBall prefab I do have a rigidbody on it and yes its true we are putting “bulletfire” to a ridigbody.
But I am still confused of why it will not work?
I tried:
bulletfire.AddForce(transform.forward * 1000);
also, and still not working
That is line 68 also,
bulletfire.AddForce(transform.forward * 1000);
Because fireBallObj is type Transform, Network.Instantiate(fireBallObj) returns an object that is a Transform, not a Rigidbody. You cannot cast, or change, a Transform into a Rigidbody.
What Intense_Gamer94 says is right.
// Assigning this in the Inspector will get you the Rigidbody instead of the Transform
public Rigidbody fireBallObj;
Then your code should work as Network.Instantiate(fireBallObj) will return an object that is a Rigidbody, which can then be put into the Rigidbody bulletfire variable.
tl;dr: GameObject, Transform, and Rigidbody are all different things! A variable can only hold a single type, not any type.
Yes you can! Your bullet should have a script attached to it as well as whatever you want to take damage.
// This script should be attached to your bullet prefab.
using UnityEngine;
[RequireComponent(typeof(RigidBody))]
public class Bullet : MonoBehaviour {
void OnCollisionEnter(Collision info) {
// Unity has detected a collision. Did we hit an enemy?
Enemy e = info.gameObject.GetComponent<Enemy>();
if (e != null) {
// Causes 10 damage.
e.TakeDamage(10);
}
}
}
// This script should be attached to enemies who can take damage.
using UnityEngine;
public class Enemy : MonoBehaviour {
public void TakeDamage(int damageAmount) {
health -= damageAmount;
if (health <= 0) {
// Out of life. Delete self.
Destroy(gameObject);
}
}
private int health = 100;
}
If you can understand all this code you’re well on your way to “getting” scripting with unity.