Object instantiated at 0, not at where I click on the screen

My objects are instantiating at x axis 0 and not where I click. As you can see in the below first image that when I Click at a point on the game screen and drag the wall (shown in Purple) from that point, after I let go of the wall at point B, the actual objects that I want are not instantiated at the wall position and along the length of the wall. Instead, you can see in the second image that the objects (shown in Purple and Yellow) are instantiated on and from a point I did not click. Is there any way to solve this? I have attached my script below. Please I need help. Thank you.

3466419--275162--Img1.jpg

 ShowMousePosition pointer;
     bool creating;
     public GameObject start;
     public GameObject end;
     public GameObject wallPrefab;
     GameObject wall;
     public GameObject polePrefab;
     public GameObject fencePrefab;
     GameObject lastPole;
     bool xSnapping;
     bool zSnapping;
     Vector3 startPos;
     Vector3 _start;
     Vector3 _end;
     Vector3 _dir;
     Vector3 pos;
     int wallLength;
     // Use this for initialization
     void Start () {
         pointer = GetComponent<ShowMousePosition> ();
     }
   
     // Update is called once per frame
     void Update () {
         getInput();
     }
     void getInput() {
         if(Input.GetMouseButtonDown(0)) {
             setStart();
             if (zSnapping.Equals (xSnapping)) {
                 zSnapping = true;
             } else {
                 zSnapping = false;
             }
             if (xSnapping.Equals (zSnapping)) {
                 xSnapping = true;
             } else {
                 xSnapping = false;
             }
         } else if(Input.GetMouseButtonUp(0)) {
             setEnd ();
             Destroy (wall);
             updateFence ();
         } else {
             if(creating) {
                 adjust();
                 //updateFence ();
             }
         }
     }
     void setStart() {
         Camera cam = GetComponent<Camera> ();
         Ray ray = cam.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit)) {
             _start = hit.point;
             //Vector3 startPos = pointer.getWorldPoint ();
             creating = true;
             start.transform.position = pointer.snapPosition (_start);
             wall = (GameObject)Instantiate (wallPrefab, start.transform.position, Quaternion.identity);
         }
     }
     void setEnd() {
         //Vector3 startPos = pointer.getWorldPoint ();
         creating = false;
               
                 Vector3 startPos = pointer.getWorldPoint ();
                 startPos = pointer.snapPosition (startPos);
                 GameObject startPole = Instantiate (polePrefab, startPos, Quaternion.identity);
                 startPole.transform.position = new Vector3 (pos.x, pos.y, pos.z);
                 //startPole.transform.position = new Vector3 (start.transform.position.x, startPos.y + 0f, start.transform.position.z);
                 lastPole = startPole;
                 //updateFence ();
       
     }
     void updateFence() {
         _dir = (_end - _start).normalized;
         for (int i = 0; i < wallLength; i++) {
                 Vector3 pos = _dir * i;
                 Vector3 worldPoint  = pointer.getWorldPoint ();
                 worldPoint  = pointer.snapPosition (worldPoint);
                 worldPoint  = new Vector3 (Mathf.Floor(pos .x), pos.y + 0f, (Mathf.Floor(pos .z)));
                 if (!pos.Equals (lastPole.transform.position)) {
                     createFenceSegment (pos);
                 }
         }      
     }
     void createFenceSegment(Vector3 current) {
             GameObject newPole = Instantiate (polePrefab, current, Quaternion.identity);
             float distance = Vector3.Distance (newPole.transform.position, lastPole.transform.position);
             Vector3 middle = Vector3.Lerp (newPole.transform.position, lastPole.transform.position, 0.5f);
             GameObject newFence = Instantiate (fencePrefab, middle, Quaternion.identity);
             newPole.transform.rotation = newFence.transform.rotation;
             //newFence.transform.localScale = new Vector3 (newFence.transform.calScale.x, newFence.transform.localScale.y, distance);
             newFence.transform.LookAt (lastPole.transform);
             lastPole = newPole;
     }
     void adjust() {
         //Vector3 startPos = pointer.getWorldPoint ();
         _end = pointer.getWorldPoint ();
         Camera cam = GetComponent<Camera> ();
         Ray ray = cam.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)) {
             _end = hit.point;
             wallLength = Mathf.FloorToInt (Vector3.Distance (_start, _end));
             end.transform.position = pointer.snapPosition(_end);
         }
         if(xSnapping) {
             end.transform.position = new Vector3(start.transform.position.x, end.transform.position.y, end.transform.position.z);
         }
         if(zSnapping) {
             end.transform.position = new Vector3(end.transform.position.x, end.transform.position.y, start.transform.position.z);
         }
         adjustWall();
     }
     void adjustWall() {
         start.transform.LookAt(end.transform.position);
         end.transform.LookAt(start.transform.position);
         float distanceo = Vector3.Distance(start.transform.position, end.transform.position);
         wall.transform.position = start.transform.position + distanceo/2 * start.transform.forward;
         wall.transform.rotation = start.transform.rotation;
         wall.transform.localScale = new Vector3(wall.transform.localScale.x, wall.transform.localScale.y, distanceo);
     }

I’ve had bad luck with Instantiate() giving things correct position, scale, rotation, or parent. Because of this, I have a habit of setting everything after calling Instantiate().

After you Instantiate on Line 60, I recommend trying the following:

wall = (GameObject)Instantiate (wallPrefab, start.transform.position, Quaternion.identity);

// if needed, wall.transform.SetParent(...)
wall.transform.position = start.transform.position;
wall.transform.localScale = Vector3.one;
wall.transform.rotation = Quaternion.identity;

There was a bug with that, in some regard, but to the best of my knowledge it’s been fixed.:slight_smile:

If you ever come across an issue, you should try:
a) using the most current final version
…b) filing a bug report

Thanks guys for the reply, but still the fences and poles are instantiating at x axis 0 and not at the wall position after I let go of the mouse button (as you can see in the above images). Any solutions to solve this?

I’m not too sure, but I think your “pointer.snapPosition()” function on line 59 might be giving you a position with x as 0. Maybe add this after line 59 to see what the “snapPosition” is when you call instantiate: "Debug.Log(“Snap Position: " + start.ToString());”

Thanks for the reply, I have entered your line of code and this is what it says

“Snap Position: StartPole (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
CreateWalls:setStart() (at Assets/CreateWalls.cs:76)
CreateWalls:getInput() (at Assets/CreateWalls.cs:41)
CreateWalls:Update() (at Assets/CreateWalls.cs:36)”

I think they must have meant start.transform.position when they said to debug start.

Opps, you’re right, I didn’t notice it was a GameObject. Try Debug.Log("Snap Position: " + start.transform.position.ToString());

Thanks for your reply, I added the line of code and this is what it displays

“Snap Position: (480.0, 0.6, 8.0)
UnityEngine.Debug:Log(Object)
CreateWalls:setStart() (at Assets/CreateWalls.cs:76)
CreateWalls:getInput() (at Assets/CreateWalls.cs:41)
CreateWalls:Update() (at Assets/CreateWalls.cs:36)”

The problem is that my fences and poles are not instantiating at the location of the wall after I let go of the mouse button.

Based on that debug log, your start position seems fine, so maybe the problem doesn’t occur until after you have already set the start position.

On line 79, you set “pos = _dir * i” where _dir is the direction of the fence and i is the segment you are on. You then create a pole at that pos position. I think you will want the position to be offset by the start point on the fence, so try changing line 79 to “Vector3 pos = _start + _dir * i”

Thank you for the reply and all your help, it finally worked. Once again thanks. I just want to ask two more questions:

  1. How do I ensure that my fence segments (Yellow objects) interact with the terrain, like slant on a hill, but the pole stays upright?

  2. If I spawn a gate, how do I make it snap only to the fence segment and swap the fence segment with the gate if I click my mouse button?

  1. You can use a technique called terrain clamping where you create a ray above your pole pointing down, then you position your pole where the ray intersects the ground using a raycast.

  2. This could be done a number of ways and is probably a bigger topic, so you may want to start a new thread if you want better help. I would have a bool or some way of knowing when you’re in “gate” mode. Only show the gate when you mouse over a fence, set the gate position to the fence position (might need slight position adjustments), and hide the fence.