How Asvarduil Eats Troublesome Scripts [Resolved!]

Resolved. Thanks all!

I don’t think you’re actually copying anything; it looks to me like you’re setting references directly. e.g.,

meshGeometry.mesh = sourceMesh.mesh;

isn’t a copy, it’s a reference. I think you want to instantiate a copy of the prefab and use that instead.

–Eric

I would normally just do an Instantiate call, but there’s one problem - my object hierarchy. It’s set up like:

Player-Map
|->Main Camera
|->Main Hand Item
|->Off Hand Item

If I Instantiate(), my sword or shield will appear outside of this hierarchy, and thus not move with the player. Further, I haven’t added in animations, but I’m planning to animate the Main Hand and Off Hand objects’ rotations and positions, so that any weapon/shield in the game will do the animations.

Main Hand Item and Off Hand Item both use the above script. To be honest, I’m happy with just reading the mesh and material off of the prefab, as long as it dosen’t wipe out my prefab.

But, that re-raises the important question: how not to do this in a way that wipes my prefab?

For this example, I made a prefab called cube… just filled with a cube…

var cube:Transform;

function Awake () 
{
	transform.Instantiate(cube,transform.position,  Quaternion.identity);
}

function Update()
{

}

Puts it at the transform’s (the script’s mesh) location. Then again, it doesn’t put it in the hierarchy right still. But you could just put a script that sets it to the same position every update.

XD Probably bad coding though, but it does position it right.

edit:
something like this is what I meant above.

var cube:Transform;
var cubeTarget:Transform;

function Awake () 
{
	cubeTarget = Instantiate(cube,transform.position,Quaternion.identity);
}

function Update()
{
	cubeTarget.position = transform.position;
}

That’s exactly the code I was talking about that gets “orphaned” by the player - it has nothing that links it into the hierarchy.

I’ve tried a few more iterations of different possibilities now; I’ve tried making a special Material and Mesh variable to reference the source material and mesh into; no luck there.

I’m thinking that Eric5h5 meant something beyond the obvious 3-argument version of Instantiate() - I don’t pretend to have a thorough mastery of the Unity JS API, I’m certainly missing something important, and I think I might find it in alternate overloads of Instantiate.

EDIT: I was right! Updating original post, and giving this a big “Resolved!” Thank you Eric5h5 and Caliber for setting me straight!