- Home /
Failure to assign a prefab to a variable
When i run this script and the objects collide unity tells me i have failed to assign my prefab correctly(giving me the error "The Variable Cubeprefab of 'Cubeinteractions' has not been assigned" the moment there is a Collision). Any one care to help the scripting illiterate?
using UnityEngine;
using System.Collections;
public class Cubeinteractions : MonoBehaviour {
public Rigidbody Cubeprefab;
void OnCollisionEnter(Collision collisioninfo){
float boxmass = collisioninfo.rigidbody.mass;
if(boxmass == rigidbody.mass){
GameObject boxgameobject = collisioninfo.gameObject;
Rigidbody Cube1 = Instantiate(Cubeprefab, transform.position, transform.rotation) as Rigidbody;
Destroy(boxgameobject);
Destroy (gameObject);
}
}
}
I assume the problem is here "public Rigidbody Cubeprefab;"
What error do you get ? We need more information on what the actual problem is . . . .
Look in the inspector and see if the variable Rigidbody CubePrefab is none or not. Of it is you must assign an object there!
Answer by Chimera3D · Nov 04, 2012 at 08:20 AM
What ZenithCode said is right, in the Inspector panel in the Unity editor window when you click on the object that you've assigned the code to it should show the name of your code, below that there should be a slot that says "None (Game Object)". Drag and drop the prefab there and it should work. When you get this error the debugger is basically telling you that you didn't put anything in the slot or didn't have anything being automatically put in there (by code).
thank you, it's taken me awhile to see what you were talking about in the inspector. However thank you for your help, probably would have given up on that whole script without it.
Your answer
Follow this Question
Related Questions
Assign prefab to variable through script 1 Answer
How to make reference from prefab to variable via C#/JavaScript? 1 Answer
How to force a variable to prefab 1 Answer
Creating a Prefab and Keeping Original Objects Script Information 0 Answers
Are there restrictions on assigning GameObjects to Prefab script variables? 3 Answers