- Home /
How to pass a class without it being a pointer/reference?
function GiveEntityGun(ent : Entity, gunItemClass : ItemClass)
{
gunItemClass = new ItemClass();
gunItemClass.exist = true;
// ... more stuff happens ...
}
gunItemClass is not meant to stay as a reference. I need it to keep its values, but become its own instance. Unfortunately, this code wipes all its values.
Can anyone please help me? I haven't been able to find the answer online yet.
to make a struct in Unityscript ...
http://answers.unity3d.com/questions/328305/does-unityscript-javascript-support-classesstructs.html
please vote UP that answer if it is useful to you, thanks
http://answers.unity3d.com/questions/261419/you-own-structs-passed-by-reference.html
Answer by Loius · Nov 07, 2012 at 07:15 PM
You need to use a 'copy constructor.'
class ItemClass {
var myVar : float;
function ItemClass() { myVar = 1; };
function ItemClass( other : ItemClass ) {
myVar = other.myVar;
}
}
Then later you can use var gic : ItemClass = new ItemClass( gunitemClass );
Your answer
Follow this Question
Related Questions
Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers
My SendMessage is problematic [SOLVED] 1 Answer
Reference a function from another object 1 Answer
Custom Documentation for Monodevelop 1 Answer
How would one use a pointer variable to reference an array element? 1 Answer