- Home /
Problem with making child an object
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.
What i meant was that secondpos is already a transform, secondpos.transform is useless.
Answer by fafase · Apr 25, 2013 at 06:04 PM
New Answer based on new comments.
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:
$$anonymous$$issingReferenceException: 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)
Did you check in the inspector that secondpos is valuated beore launching the game ?
Did you declare the variable somewhere? You should know better than me what is secondpos and where it is.
Did you try to debug it ? Put a breakpoint before using secondpos and have a look at its value.
Answer by Vonni · Apr 25, 2013 at 05:48 PM
Remove yield, and remove all the unneccesary transform calls
#pragma strict
var secondpos : Transform;
function Start () {
yield WaitForSeconds(24.0);
Destroy(GetComponent(SmoothLookAt));
transform.rotation = Quaternion.identity;
transform.parent = secondpos;
transform.localPosition = Vector3.zero;
}
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?
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Camera follow ball along cylinder 1 Answer
Moving parent to position of child 0 Answers
how to stop child object from twitching? 1 Answer
Transform child object over time and then destroy it. 1 Answer