Transform Constructor

Could someone please show me how to make a constructor with a transform in it’s properties and how to set the transform in it’s instances.

thanks. :slight_smile:

1 Answer

1

In terms of programming nomenclature, a constructor doesn’t have “properties”. A property is something a class has. A class has fields/class variables, and a property in .Net is a set of (or a single) accessor methods, called get and set, that work with that field. A constructor is said to “take arguments”, and it executes code in its “body”.

I’m not sure what use you’d have for this, but I’m going to answer your question verbatim. This is a constructor, that takes a transform and sets a property when you make instances of it:

public class SomeObject
{
    // Declare a transform as a property
    public Transform TransformAsAProperty { get; set; }

    // Declare a constructor with a transform as an argument
    public SomeObject(Transform argument)
    {
        // Use the argument to set the property. Referencing a property in this manner automatically calls its set-method
        this.TransformAsAProperty = argument;

        // Work with the transform's variables
        TransformAsAProperty.position = new Vector3(1, 2, 3);
        TransformAsAProperty.rotation = Quaternion.Euler(1, 2, 3);
        TransformAsAProperty.localScale = new Vector3(1, 2, 3);
    }
}

thanks but how do i make a variable with it

A Property, when declared like the above, auto-implements the variable that it works with. See http://msdn.microsoft.com/en-us/library/bb384054.aspx for more information. When you declare properties like so, you don't have to make a variable yourself for the property to work with. It happens behind the scenes.