Hello!
I want an object to be a child of an other when 24 seconds pass.
Heres the script. It seems fine to me, but it doesn’t work. Any help?
#pragma strict
var secondpos : Transform;
function Start () {
yield WaitForSeconds(24);
Destroy(GetComponent(SmoothLookAt));
yield;
transform.rotation = Quaternion.identity;
transform.position = secondpos.transform.position;
gameObject.transform.parent = secondpos.transform;
}
function Update () {
}
Have you check in the inspector that secondpos is valuated ? secondpos is already a transform, I don't think you need to do secondpos.transform on lines 9 and 10
What lines 9 and 10 are doing are making sure that the child object is directly lined up with the parent object. Whats the 'yield' on line 7 for? That might be where youre going wrong. A good form of practice debugging is adding in a Debug.Log(); and place either the variable or a string to verify if the line is being executed. Place it before and after where ever between the code and see if its working. If nothing responds in the Debug, then you know where your problem starts.
But the object you have this script on is probably a character controller / rigidbody. (Am I right?). If so, linking wont do much. It only helps the rigid object inherit movement of parent if its sleeping I think.
Actually it's a camera. the project is a presentation. The script doesn't work, it moves he camera to 0,0,0 world position without being a child to he othe object. Does it matter that the "secondpod" is already a child to a third object?
You say the camera moves with an animation to the empty game object and attach itself to this. Let’s see if we can work this out.
var secondPos:Transform;
function Start(){
StartCoroutine(CamAnim());
}
function CamAnim(){
animation.Play("CamAnimation");
while(animation.IsPlaying("CamAnimation")){
yield;
}
transform.parent = secondPos;
}
Since you have the animation done, it probably include already all movement and rotation so your camera should end up pretty much ready to go.
If not then you need to add some little details before and after the parenting, like you did in your example.
It doesn't work, the console says this: MissingReferenceException: The variable secondpos of 'Js_CameraChange' doesn't exist anymore. You probably need to reassign the secondpos variable of the 'Js_CameraChange' script in the inspector. Js_CameraChange.ToBeDoneLater () (at Assets/Js_CameraChange.js:10)
Have you check in the inspector that secondpos is valuated ? secondpos is already a transform, I don't think you need to do secondpos.transform on lines 9 and 10
– KiraSenseiWhat lines 9 and 10 are doing are making sure that the child object is directly lined up with the parent object. Whats the 'yield' on line 7 for? That might be where youre going wrong. A good form of practice debugging is adding in a Debug.Log(); and place either the variable or a string to verify if the line is being executed. Place it before and after where ever between the code and see if its working. If nothing responds in the Debug, then you know where your problem starts.
– GC1983What i meant was that secondpos is already a transform, secondpos.transform is useless.
– KiraSensei