- Home /
Creating object as a child in group by script
Hello guys! please can some 1 help me? a need do that: if collision is detected--->create variable Gunspawn(object)into variable placespawn(hi's location in group of object(child))
var destroy :AudioClip;
private var destroyaudiosource:AudioSource;
private var isPlayingSound:boolean;
var Gunspawn: GameObject;
var placespawn: Transform;
function Start(){
destroyaudiosource=gameObject.AddComponent(AudioSource);
destroyaudiosource.clip=destroy;
destroyaudiosource.loop=false;
destroyaudiosource.volume=1.0;
}
function OnCollisionEnter(Gun : Collision){
if (Gun.gameObject.tag == "gun") {
isPlayingSound=true;
destroyaudiosource.Play();
//Destroy (gameObject);
please help me what i need to write here for creating existed object in group as chil?
}
}
Answer by rutter · Apr 19, 2012 at 08:16 PM
You can make one GameObject a child of another GameObject by setting its `transform.parent` property. The page I just linked has a few simple examples. Is that what you're looking for?
Answer by Dmitri · Apr 19, 2012 at 10:28 PM
Yes its work! thx you verry much!!! but it write a problem (NullReferenceException: Object reference not set to an instance of an object)
if some 1 need to do what i write in description script need to be like that: function OnCollisionEnter(Gun : Collision){
function OnCollisionEnter(Gun : Collision){
if (Gun.gameObject.tag == "gun") {
GunpositionTransform.parent=transform;
GunpositionTransform.localPosition = Vector3(-3.122956,-0.01368039,0.04606679);
}
} //vector3(set your position where you wonna place that child axis xyz)
Your answer
