- Home /
[Closed] Prefab not instantiating, No errors.
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");
//}
}
}
I'm surprised that has no errors. The instantiate will return a GameObject, which is not a Rigidbody, so clone should be null. The standard use of instantiate is:
Type thing = (Type)Instantiate(original);
, where 'Type' is ALSO 'original's type.
Have you tried:
clone = (Rigidbody)Instantiate(Find("yuck").rigidbody);
Is your debug printing? Try positioning your new object at origin and then use UnityEditor.EditorApplication.isPaused to observe it.
You're cloning an enemy. Does it clone in such a state that it immediately destroys itself (because it's out of health or something)?
Answer by drudiverse · Jul 21, 2014 at 04:49 AM
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++;
}
}
}
I'm trying to use rigidbody for a cubic enemy that comes towards you, then detect if it is touching the player, If so, Run the game over script.
i program in javascript, and it only instantiates gameObjects, because the rigidbody is attached to the GO by default, or can be added with AddComponent. a rigidbody is always a component of a GO.
you could try...
var thisenemy: find game Object $$anonymous$$ain enemy
instantiate (thisenemy.rigidbody, position, ...
Answer by ConsciousCoder · Jul 21, 2014 at 04:56 AM
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"); //} }
I think you have one too many brackets "}" at the end of the script, try deleting one at the end of the script and make sure theirs only 2 sets of brackets in the script.
Answer by tyrike thompson · Jul 27, 2014 at 06:37 AM
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");
//}
}
}