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.
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.
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?