using UnityEngine;
using System.Collections;
public class Cloning : MonoBehaviour {
// Update is called once per frame
void Update () {
//float rndizer = Random.Range(1, 101);
//if (rndizer == 1) {
Rigidbody clone;
clone = Instantiate(GameObject.FindGameObjectWithTag("MainEnemy"), transform.position, transform.rotation) as Rigidbody;
clone.AddForce(Vector3.back);
Debug.Log("Success spawning enemy");
//}
}
}
you are trying to clone a gameObject as a rigidbody…
GameObject.FindGameObjectWithTag(“MainEnemy”) will return a GO.
you can’t clone a gameObject as a RigidBody… the rigidbody is the physics element of the gameObject that defines it’s mesh as a rigid entity.
you need to instantiate a Transform or a GO. … rigidbody, is used for bullets or something. i had never seen rigidBodies instantiated exclusively, if someone can explain why you have to define the componenty type being instantiated in C i would be happy.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform prefab;
void Example() {
int i = 0;
while (i < 10) {
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
i++;
}
}
}
You can try creating a cube or an empty game object and add a rigid body component to it and dont forget to tag it. Then drag this new object into the empty public clone variable under the script component in the inspector pane.
-Also you should create a public variable of type Rigidbody to store the prefab.
public Rigidbody clone;
void Update () {
//float rndizer = Random.Range(1, 101);
//if (rndizer == 1) {
//Rigidbody clone;
clone = Instantiate(GameObject.FindGameObjectWithTag(“MainEnemy”), transform.position, transform.rotation) as Rigidbody;
clone.AddForce(Vector3.back);
Debug.Log(“Success spawning enemy”);
//}
}
The only option is to Instantiate the object and store it in a GameObject type variable and access the rigidbody later. I had this problem also when I tried Instantiating as a Transform.
Try :
using UnityEngine;
using System.Collections;
public class Cloning : MonoBehaviour {
// Update is called once per frame
void Update () {
//float rndizer = Random.Range(1, 101);
//if (rndizer == 1) {
GameObject cloneObject;
Rigidbody clone;
cloneObject = Instantiate(GameObject.FindGameObjectWithTag("MainEnemy"), transform.position, transform.rotation);
clone = cloneObject.GetComponent<Rigidbody>();
clone.AddForce(Vector3.back);
Debug.Log("Success spawning enemy");
//}
}
}