- Home /
Instantiate as a child?
I have this script, and I want "tempbrick" to be parented to the object this raycast hits. How? I only understand that you have to instantiate, then parent the new object to the one I need. I just don't know how to declare tempbrick's transform as a variable.
var ray : Ray;
var hit : RaycastHit;
var objectBounds : Bounds;
var maxNumberOfClones : int;
var tempbrick : GameObject;
private var numberOfClose : int;
private var activeClones : Array = new Array ();
function Update() {
print(tempbrickTransform);
if (Input.GetButtonDown ("Fire1")) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
objectBounds = hit.transform.renderer.bounds;
Debug.Log(objectBounds);
var rotationOfHitObject : Quaternion = hit.collider.gameObject.transform.rotation;
Debug.DrawLine (ray.origin, hit.point);
activeClones.Add(Instantiate(tempbrick, hit.point, rotationOfHitObject));
if(activeClones.length > maxNumberOfClones) {
Destroy(activeClones[0]); //destroy the first entry of the array
activeClones.Shift(); // remove this entry from the array
}
}
Physics.Raycast (ray, hit);
}
Debug.DrawRay (ray.origin, ray.direction * 10, Color.white);
}
Answer by SilverTabby · Jul 21, 2011 at 06:15 AM
There's something about what you're trying to do here makes me really want to see this work (I smell a good idea?). I'm going to take a shot at doing this from scratch:
#pragma strict
var maxNumberOfClones : int;
var brickPrefab : GameObject;
private var activeBricks : GameObject[]; //The Array class is evil, use this instead
private var carrPos : int = 0;
private var ray : Ray;
private var hit : RaycastHit;
private var instanBrick : GameObject;
function Start()
{
activeBricks = new GameObject[maxNumberOfClones];
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log(ray);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.white);
if(Physics.Raycast(ray, hit))
{
instanBrick = Instantiate(brickPrefab, hit.point, hit.transform.rotation) as GameObject;
instanBrick.transform.parent = hit.transform;
addToActiveBricks(instanBrick);
Debug.Log(hit);
Debug.Log(hit.transform);
Debug.Log(instanBrick.transform);
}
}
}
function addToActiveBricks(input : GameObject)
{
//if(activeBricks[carrPos] != null) //unsure if needed
Destroy(activeBricks[carrPos]);
activeBricks[carrPos] = input;
carrPos++;
if(carrPos >= maxNumberOfClones)
carrPos = 0;
}
@Script AddComponentMenu("Test Scripts/Give SilverTabby Cookies")
I don't even know if that will compile but it should work (it did in my mind at least)
Hope this helped <("<)(^"^)(>")>
Oh dear lord it worked...
You uh, I feel bad about abusing the system and squeezing this successful code out of you. I'm not sure how to go about this...
Anyway, if I'm successful with this whole project, you might just see it come to fruition. I might even make a little inside reference to this whole incident. Thank you with the BREADTH OF A THOUSAND WORLDS.
It's less of you abusing the system and more of you found a random guy online who had too much time on his hands and a disposition for helping people.
But an up vote and a check would be nice though :P
On topic: I think the problems in you script were that
1) You were using the Array
class (I have NEVER gotten it to work)
2) You were not using "#pragam strict
" (It won't compile if you aren't explicit about EVERYTHING, very useful for tracking down casting errors. It can also improve run-time efficiency by up to 2000%)
3) When you were calling Instantiate()
, it returns an OBJECT, but the function you were passing it into was looking for a GA$$anonymous$$EOBJECT. All you were missing was "as GameObject
" at the end of Instantiate()
.
Really that's it :) You're logic was O$$anonymous$$, you were just fighting the compiler without knowing it.
Answer by SilverTabby · Jul 20, 2011 at 04:42 AM
I was trying to be vague so that you would figure it out on your own, but obviously I'm being too vague.
Here's a fish:
var instanceOfTempBrick = Instantiate(tempbrick, hit.point, rotationOfHitObject);
tempbrick.transform.parent = hit.transform;
activeClones.Add(instanceOfTempBrick);
//some code omitied
"Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime."
The teaching is in my comments ( well at least I tried to teach in the comments :\\ ).
Hope this helped <("<)(^"^)(>")>
I figured that. I'll try to do the rest on my own. Just one last question: does the order in which I declare variables and specify events count?
a + b == b + a
Generally, order does not mattter, as long as a variable exists when you are doing something to it.
Though you should still be careful because the one time it does matter, it's easy to miss.
Ok. And when you say "some code omitted", do you mean in the line itself, or the bits and pieces above and below that one line? As I have it now, it seems like it should work. Only it doesn't.
I'm a bit stressed about this because this code is the absolute basis of this entire project. I can't really get started until this is complete.
It's the bits and pieces above and below that one line. Not only that but those bits and pieces are the code you posted in the original question.
I'm going to copy-paste some advice I gave in another question:
Get some sleep. Go to a movie. Take some friends out to dinner.
Take your $$anonymous$$d off of it. I've solved more program$$anonymous$$g problems in
this way than I have pouring over scripting references. It works.
This is probably is the last thing you want to hear(read?), but sometimes It's better to just open a blank file and try again. Retype the bits that worked and restart the bits that don't work. Less is more.
I had a giant mess of code with classes inside of classes and more variables than you could count. It sort of worked. Then we destroyed a motor because of it. I opened a new file, redid everything, cut off 80% of the code, and it worked like a charm. Never had another problem with it. Less is more.
Answer by SilverTabby · Jul 19, 2011 at 05:21 AM
if( Physics.Raycast(ray, hit) )
tempbrick.transform.parent = hit.transform;
http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html
http://unity3d.com/support/documentation/ScriptReference/RaycastHit-transform.html
Hope this helped
"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
I put "tempbrick.transform.parent = hit.transform;" right after "Debug.Log(objectBounds);".
If tempBrick is the top node in an instance, it will work fine. You definitely CAN make an instance the child of a transform in another prefab's instance.
[Edited for clarity]
Oh you need to instantiate the prefab first. The data corruption message means you are trying to edit the template (prefab) of an object ins$$anonymous$$d of the instance of the object (The GameObject returned by Instantiate)
Just replace tempbrick with Instantiate(tempbrick, location, Quarternion.Identity).transform.parent = //...
Well, apparently here it isn't working. I'm considering it's because it's trying to somehow parent the object before it's even instantiated. I've seen other questions almost exactly like this, only they don't involve the clone limitation that's implemented here. Any thoughts?
EDIT Oh, I'll try the above.
Which "tempbrick" am I supposed to replace that with? And what do mean by "location"?