Because you aren’t accessing the transform property. The behavior isn’t the object the behavior is attached to itself. The behavior is a Component of a GameObject. The Component shares the transform object of the GameObject that contains it.
Assets/_World Map/Scripts/RayCaster2.cs(22,27): error CS0117: UnityEngine.Transform' does not contain a definition for relativePos’
when
transform.relativePos = Vector3.zero;
or
this.transform.relativePos = Vector3.zero;
or
this.gameObject.transform.relativePos = Vector3.zero;
or
the last one
ok let me clear out one thing, for performance issues i wanted ( i am in another code now …) i wanted to create the object before i get into a function, so for example, if i need to use that vector in a loop, the code will not create dump objects and erase them.
I want to create one object and alter its values during the loop. so i made sth like this:
// Instantiates a prefab in a circle
GameObject prefab ;
int numberOfSoldiers = 20;
int radius = 5;
Vector3 pos = new Vector3 (0.0f,0.0f,0.0f);
void Start () {
for ( int i = 0; i < numberOfSoldiers; i++) {
float angle = i * Mathf.PI * 2 / numberOfSoldiers;
pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
Instantiate(prefab, pos, Quaternion.identity);
}
}
however unity wants me to define the pos objects again, but it is defined?
it says
Expression denotes a `type', where a `variable', `value' or `method group' was expected
dont i define it before the Start function ?
can someone explain me what i am missing now?
I think you left out the “new” keyword in front of Vector3, you may also need to wrap the constructor call in parenthesis before the multiplication:
pos = (new Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle))) * radius;
Also, I don’t think you’re saving any time by defining “pos” outside the loop (I’m guessing that was the object you were referring to) if you’re re-instantiating it each time. Instead it’d have to be something like:
Vector3 is a struct, not a class. This has many consequences, one of them being that calling ‘new Vector3’ does not create a new instance in memory. Instead it is created on the stack, which makes vector3 objects very cheap to create. Avoiding ‘new’ with structs will not gain you anything, ever.