Defenders Spawning at Random places

DefenderSpawner.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class DefenderSpawner : MonoBehaviour

{

Defender defender;

private void OnMouseDown() {

   SpawnDefender(GetSquareClicked());

}

public void SetSelectedDefender(Defender defenderToSelect) {

    defender = defenderToSelect;

}

private Vector2 GetSquareClicked() {

   Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

   Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);

   Vector2 gridPos = SnapToGrid(worldPos);

   return gridPos;

}

private Vector2 SnapToGrid(Vector2 rawWorldPos) {

   float newX = Mathf.RoundToInt(rawWorldPos.x);

   float newY = Mathf.RoundToInt(rawWorldPos.y);

   return new Vector2(newX, newY);

}

private void SpawnDefender(Vector2 worldPos) {

Defender newDefender = Instantiate(defender, worldPos, Quaternion.identity) as Defender;

}

}

DefenderButton.cs
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class DefenderButton : MonoBehaviour

{

[SerializeField] Defender defenderPrefab;

private void OnMouseDown() {

  var buttons = FindObjectsOfType<DefenderButton>();

  foreach(DefenderButton button in buttons) {

      button.GetComponent<SpriteRenderer>().color = new Color32(41, 41, 41, 255);

  }

  GetComponent<SpriteRenderer>().color = Color.white;

  FindObjectOfType<DefenderSpawner>().SetSelectedDefender(defenderPrefab);

}

}

My defenders seem to spawning at random locations


Some help would be wonderful
Thank you

Hi,

Welcome to our community! :slight_smile:

Could you please format your code to increase its readability? At the moment, I find it fairly difficult to read.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Also check the animation and make sure the position of the defenders does not get animated.

Hopefully, this helped. :slight_smile:


See also:

hey there,
my guess is that when you’re instantiating the Defender prefab you have the vector coordinates as worldPos, and not the vector that SnapToGrid() returns.
So i would change the vector value worldPos to the function you created.
hope that helps

Edit:
reading back what i wrote, you should pass the worldPos to the SnapToGrid() function when you instantiate the defender; because the worldPos you pass in to the SpawnDefender() function is the rawWorldPos.

Edit edit:
Sorry about the many replies (i’m new at this too and thought i could help).
i compared your code to mine, and it seems like there’s an extra step that i believe is covered in the following lectures - where you check if you have sufficient funds to place a defender.

Also, on the screenshot you provided, it says the object you are trying to instantiate is Null. Can you click on that error and find the line of code that is creating that error? that might help you figure out what’s going on.

good luck with the bug.

Thanks for the help

Were you able to figure out the problem?
If not, I had an issue similar to what you described, and i realized that the RigidBody type should be Kinematic and not Dynamic. Dunno if that’s any help.