Instantiate woes

Hello all,

I created a prefab to instantiate it, but a reference to a script is not being instantiated and I’m getting this null reference error!

Attached is an image of the inspector.

variable animal has a none(Mono behaviour) reference to it. I tried dragging the script to it but it won’t take it. the object that I used to create the prefab has the mono behaviour attached but the created prefab don’t want to attached it.

Below is the script for the collision.

var target : Rigidbody;
var gameobject = 0;
var animal : Score;

function OnCollisionEnter (collision:Collision)
{
	if(target)
		{
			gameobject++;
			animal.animals += gameobject;
			Destroy(gameObject);
		}
}

on the previous level, the object is not being instantiated and everything works just fine. I don’t understand what’s going on?.

Thanks Again for showing me the light bulb!
Ray

16520--543--$prefab_167.jpg

It sounds like you’re getting a null reference error because animal doesn’t refer to anything.

I’ve had trouble with this as well. I found that the trick to attaching that object to your script is as follows:- In the inspector, click on the animal element to bring up the list of objects to attach to it.

  • If the object/script you want to use does not appear in the list, select one of the MonoBehaviour items listed
  • Double-click on the name that appears in its place (should say something about MonoBehaviour)
  • This should change the inspector view to a new item and now you can drag any script or MonoBehaviour object to it.I’ve found that this occasionally doesn’t work when working with prefabs, but you should be able to set it up once you Instantiate it by assigning it in the script that creates it.

Hi Spaniard,

Tried attaching it but it won’t let me, If i click on the animal I have the reference in there but it still won’t let me attached it.

hmm, a bug perhaps?

You mean I have to set it up again in the instantiate script?

Thanks,
Ray

When you Instantiate the prefab, you could assign the animal as you need:

var instance:GameObject = Instantiate(prefabVar, position, rotation);
instance.GetComponent("Collision").animal = animalScriptObject;

I haven’t tested that code, but that’s roughly what you could do in JavaScript.

You may be able to make an instance of the prefab in the scene and then add the script through the method I described above and then update the prefab by either using the menu item (I can’t remember the menu name right now) or dragging the object back to the prefab.

Are you dragging and dropping the “Score” script or an object with a Score script attached? Animal is a type of Score. If the object you are dragging does not have a Score script then I do not think it will attach.

Score is a script that will receive data to add 1 to the # of gameObjects.

I removed the collision script from the prefab and included it in the instantiate scripts, but now it won’t let me destroy the instantiated object.

The error is saying “Destroying assets is not permitted to avoid data loss”

Now How do I destroy the cloned object?
below is the script

var clone : GameObject;
var state : boolean;
state = true;
var target : Rigidbody;
var animal : Score;
var gameobject = 0;
	

function Update()
{
		if(state == true)
		{
			Instantiate(clone, transform.position, transform.rotation);
			clone.transform.Translate(Vector3.left*Time.deltaTime);
			state = false;
		}
}

function OnCollisionEnter(collision:Collision)
{
	if(target)
		{
			state = true;
			gameobject++;
			animal.animals += gameobject;
			Destroy (clone );
			
		}
}

Sorry for being a dum dum. I guess it’s part of learning
:slight_smile:

Thanks again,
Ray

It looks like your Destroy() call is trying to destroy the prefab. I believe you need a variable to hold the object that you create

// Variable to hold the object you create
var theClone : Transform;
var clone : GameObject; 
var state : boolean; 
state = true; 
var target : Rigidbody; 
var animal : Score; 
var gameobject = 0; 
    

// Update() function assigns the clone to the variable created above
function Update() 
{ 
      if(state == true) 
      { 
         theClone = Instantiate(clone, transform.position, transform.rotation);
         // .transform is not needed if theClone is a Transform
         theClone.transform.Translate(Vector3.left*Time.deltaTime); 
         state = false; 
      } 
} 

// You now destroy the object held in the variable added (theClone)
function OnCollisionEnter(collision:Collision) 
{ 
   if(target) 
      { 
         state = true; 
         gameobject++; 
         animal.animals += gameobject; 
         Destroy (theClone ); 
          
      } 
}

allright,

looks like it’s destroying it, but my score game controller is not adding the score right. :lol:

I’ll try to get some sleep and bang my head again tommorow.

Thanks, appreciate it very much
Ray

Prefabs do not keep references to other objects outside the prefab when instantiated. This is not a bug, but a design decision. – The reference could have been an object in a whole another scene, therefore it is not saved in the prefab.

Good to know that it’s not a bug.

Bad for me cause that means I need to really work a little bit harder! :slight_smile:

Hmm, I tought it was destroying it but it’s not. :sweat_smile:

using

 var theClone: Transform;

including and not including any .transform gives me InvalidCastException:Cannot cast from source type to destination type.
and the prefab just gets cloned with out stopping.

Tried var theClone:GameObject and did not get any error but I still can’t destoy the clone.

The clone has a box collider, also tried using the object as a place holder as well as a empty GO(cube)

It seems that the object gets cloned but the clone does not inherit the behaviour of the original?

If I shoot at the clone, explosion happens but not getting destoyed and score just tells me that it’s a miss, If I shoot at the placeholder it gets destroyed as long as if it has a box collider.

If the pellet explodes when it hits the clone, then the clone detects collision but not Destroy.

Sorry for being so dum but this instantiate/destroy clone is forcing me to swear.

Thanks,
Ray

P.S. Revenge of the clone

Do you have a rigid body on each of the two objects that are colliding? It looks like both objects have to have a rigid body attached where isKinematic is false (unchecked).

Maybe one of the Otee guys can explain why.

Okay, so far I was able to destroy that prefab by attaching a separate OnCollision function to the prefab to destroy itself.(I’m such a big DUH!)

Now another question.

Since prefabs do not reference other objects outside the prefab when instantiated, how can I tell a global variable from another script(myscorescript) that this prefab was destroyed and add 1 to total gameobjectsdestroyed.

I was trying like

var prefab : PreFabCollision;
var prefab1 = 0.0;

then somewhere in the script access the var from prefab
prefab1 = prefab.gameobject;

not getting errors but not getting that the prefab was destroyed either.
Is what I’m trying to do with the prefab doable or not doable.?

Thanks again,
Ray

What you may want to do is have your script have an unassigned variable to hold a reference to your controller object. Then, in your start function you would assign the reference.

var controller : YourControllerScript;

function Start() {
  tempObject = GameObject.Find("NameOfYourControllerObject");
  if (tempObject != null) {
    controller = tempObject.GetComponent(YourControllerScript);
  }
}

Then when you go to destroy your object

function OnCollisionEnter(other : Collision) {
  ....
  controller.IncrementScore();   // Function to add one the Score in the Controller
  Destroy(this);
}

[/list]

Actually you can keep references to any other prefab or any other asset in the project folder. You just cant have a reference from a prefab to an object inside of a scene.

If you need a reference to an object in your scene, then what you probably want is to have the game object in your scene and not in a prefab.

@lfrog

I was doing a similar approach with your example, works okay as long as it’s not a prefab.

@Joe

Works! :smile: