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);
}
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)