vector3 question

I made sth like this

public class RayCaster2 : MonoBehaviour {


	public Vector3 relativePos;

	void Start () {

		relativePos.x = 0;
		relativePos.y = 0;
		relativePos.z = 0;
	
	}

before using relativePos.x, relativePos.y relativePos.z, i tried :

relativePos = Vector3 (0,0,0); // did not work
relativePos.Vector3 = Vector3 (0,0,0); // did not work

and i did not understand why it didnt work, what was the correct way?

Thank you

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.

Give this a shot:

transform.relativePos = Vector3.zero;

This is the same as:

this.transform.relativePos = Vector3.zero;

Is the same as:

this.gameObject.transform.relativePos = Vector3.zero;

Is the same as:

gameObject.transform.relativePos = new Vector3(0,0,0);

Short version: In C#, you must put “new” before any class constructor (unlike UnityScript where it’s always implied).

that too.

thank you :slight_smile:

i get the error

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

:frowning: am i missing sth?

Honestly, do you see ANYWHERE that Transform component have a native variable called relativePos ?

The answer is : no. So, what do you expect to get when you write transform.relativePos ?

relativePos is a variable you created yourself…

yes i agree, thats why i tried
relativePos = Vector3 (0,0,0); // did not work

but it does not work, i am missing sth and trying to get it :frowning:

As starmanta already pointed out, you need to use ‘new’ in C#:

relativePos = new Vector3 (0,0,0);

i am ashamed guys… sorry :frowning:

Sorry for the transform misinformation. I misunderstood what you were asking. I thought you were trying to rotate the current object.

i really like everyone in here, everybody is helpful. this is a real community :slight_smile:

Yeah… I’m pretty surprised myself. I’m brand new, and I’ve yet to see anyone that’s a bigger jerk than myself. That’s a good thing. =D

haha :slight_smile:

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?

thank you very much

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:

pos.x = Mathf.Cos(angle) * radius;
pos.z = Mathf.Sin(angle) * radius;

Also, make sure that your “prefab” is assigned (it probably is, just can’t see it)

Finally, be sure to benchmark the time difference – unless you have a lot of soldiers, I don’t think you’ll see much of a change.

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.

thank you very much

I know you can create a class, by writing :

class Example
{

}

class ExampleOther extends MonoBehaviour
{

}

Is there a way to create a struct ?

Replace “class” with “struct”.

–Eric