getting a error for transform.position = ObjectA.transform.position;

this code:

using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	public GameObject ObjectB;
	public GameObject ObjectA;
	transform.position = ObjectA.transform.position;
}`  

is giving me an error. The error is “UNEXPECTED SYMBOL ‘=’ in class, struct, or interface member declaration”. this is on the transform.position = ObjectA.transform.position; I have looked every where but can not find out how to get rid of this error. Thanks in advance.

1 Answer

1

You can only give a default value for class member variables you are defining. tranform is an inherited property and you should set it’s value when an instance of the class is initialised (in Awake or Start).

public class NewBehaviourScript : MonoBehaviour {
    public GameObject ObjectB;
    public GameObject ObjectA;

    void Start() {
        transform.position = ObjectA.transform.position;
    }
}

yes you should write transform.position = ObjectA.transform.position; in any of the function. You cannot write that line where variables/objects are declared.

thank you but how would i make it every time I spawn one make it go strait to objectA. thank you so much. I have an object called bullet that is meant to spawn right on the Object called doombot but so far it spawns in the corner of my screen (no idea why this happens). how would i make it spawn right at the doom bot or transport to the doombot right as it is created. thank you

If I understand correctly, something like this could work. Set doombot's tag as "doombot" and have this code in a script which is attached to the bullet. void Start() { transform.position = GameObject.FindWithTag("doombot").transform.position; }