Find Object in Prefab

function OnTriggerEnter (other : Collider) {
    // Does this truck (Prefab instance) hits a truck
    // beloning to another ID (spawnpoint)?
    // the other car should then wait.
	
    if (other.CompareTag ("truckBoundary")) {
 		othergameobj = other.gameObject; 
 		otherID = getOtherID(othergameobj);  //query the other object id.
   		var wagenInPrefab = othergameobj.Find("Vrachtwagen");
		   var theScript : vehicle = wagenInPrefab.GetComponent("vehicle");
           if (ID!=otherID) {
             theScript.haltThisVehicle(); // works, but wrong object halts!
             }
..
..

The above script; am I actually refering to the “carTruckBoundary” script inside the clone I have just instantiated?, or the general object?

The problem is that I am trying to insert behaviour so that one car stops for another car, until the other car has passed. I have ‘spawnpoints’ with an unique ID, so when cars of spawnpoint A collide with B, one of the two halt.

Wrong cars (the last generated one) stops instead of the correct one, so my guess is that I am not referring to the correct prefab.

GameObject.Find() finds an object in the scene, globally - even if you’re calling gameObject.Find() it still refers to the global, static function.

Transform.Find does what you want:

         var wagenInPrefab = othergameobj.transform.Find("Vrachtwagen");

note that it returns the transform of the object, rather than the gameObject itself; in your posted code it doesn’t make a difference but you might want to add .gameObject to the end of that line if you need to use functions like .AddComponent that Transform doesn’t have.

Okay thanks! Not entirely logical to me (confusing) but good to know!

When a prefab is instantiated, and this prefab contains scripts that controls its own behavior, are the scripts also instances?

I mean this:

  • myPrefab
    – some child object
    – some other child object
    — some script attached to child object

Is the script inside the prefab an instance or does the prefab refer to a global script?

Because when I do this:

// inside OnTriggerEnter
othergameobj = other.gameObject;
var ctbInPrefab = othergameobj.transform.Find("truckBoundary");
var theScriptC : carTruckBoundary = ctbInPrefab.GetComponent("carTruckBoundary");

… I get a NullReferenceException!
Is this because I am refering to transform now? But how do I get back to ‘gameObject’?

carTruckBoundary is a script attached to the child object “carTruckBoundary”,

yes.

If you get a null exception there can be two causes:

  1. finding the transform fails because there is no child transform with that name.
  2. There is no script of that type attached to the child

If there is only one carTruckBoundary script on each prefab, why not just use:
othergameobj .GetComponentInChildren(carTruckBoundary);

Btw. why do you pass a string to get component, just pass the scriptname instead without quotes.

Ah okay, that’s a lot easier Joachim! Thank you.
I have narrowed it down to the following code:

var ID : int = 0;

function OnTriggerEnter (other : Collider) {
	// Raakt een truck een bocht? Draaien zoals aangegeven! 	
    if (other.CompareTag ("truckBoundary")) {
 	
 		othergameobj = other.gameObject; 
 		truck_script = othergameobj.GetComponentInChildren(carTruckBoundary); 
 		vehicle_script = othergameobj.GetComponentInChildren(vehicle); 

 		otherID=truck_script.ID;
 		 		
   	 	if (ID!=otherID) {
   	 		// deze prefab (niet other) niet meer laten rijden.
	   	 	Debug.Log("Two trucks belong to different spawns collide");
			vehicle_script.haltThisVehicle();
   	 		}
   	 	else {
	   	 	Debug.Log("Two trucks of same spawn collide");
    		vehicle_script.haltThisVehicle();
   	 		}
   		}  
    }

But, NullReferenceException; Object reference not set to an instance of an object when I am calling: vehicle_script.haltThisVehicle();

How can I refer to an instance?

Sorry for all the questions; the first Unity game for a client is a reality!

That just means there is no script of that type in any of the children.

Add the following before the GetComponent call:
Debug.Log(“colliding with someone”, other);

In the console you can select the error now and it will draw a line to the object which you are attempting to get the component from. You will find that it has no carTruckBoundary in any of its children.

       othergameobj = other.gameObject; 
       truck_script = othergameobj.GetComponentInChildren(carTruckBoundary); 
       vehicle_script = othergameobj.GetComponentInChildren(vehicle);

why not just:

       truck_script = other.GetComponentInChildren(carTruckBoundary); 
       vehicle_script = other.GetComponentInChildren(vehicle);

Aha! I see it now.
I am referering to a child, but actually I need to get the parent, the parent object from ‘other’

- VehiclePrefab 
--- Aanhanger 
--- Vrachtwagen (I NEED TO REFER TO THIS OBJECT AND ACCESS COMPONTENT X) 
------ truckBoundary (THIS ONE IS REFERED TO FROM ONTRIGGERENTER: 'other')

Now I know the child, can I still access it’s parent and the component inside?

transform.parent and transform.root are your friends.