I created a simple ground object and attached the below script to it. Basically whenever the player interacts with it, i want it to spawn the prefab at its location.
This works until i duplicate the object in the hierarchy and from then it only instantiates the prefab at the location of the last duplicate.
It’s probably really simple, regardless any help would be appreciated, thanks guys.
using UnityEngine;
using System.Collections;
public class DiggableDirtScript : MonoBehaviour
{
public bool hasBeenDug = false;
SpriteRenderer dirtSprite;
Transform dirtTransform;
public GameObject dugUpDirtPrefab;
// Use this for initialization
void Start ()
{
dirtTransform = transform;
dirtSprite = gameObject.GetComponent<SpriteRenderer>();
}
public void DugUp(bool dug)
{
hasBeenDug = dug;
if (hasBeenDug == true)
{
//Destroy(dirtSprite);
dirtSprite.enabled = false;
Instantiate(dugUpDirtPrefab, dirtTransform.position, dirtTransform.rotation);
}
else
{
dirtSprite.enabled = true;
}
}
}
This is the player script
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public LayerMask playerMask;
float playerSpeed = 1.5f, playerHeight, playerWidth;
GameObject[] objectInFront;
bool facingUp, facingDown, facingRight, facingLeft;
Transform playerTransform;
Rigidbody2D playerBody;
AnimatorController playerAnimController;
//Scripts
GameObjectHandler objectHandler;
DiggableDirtScript dirtScript;
//GameObjects
public GameObject gameMaster;
private GameObject dirtTempObject;
// Use this for initialization
void Start ()
{
SpriteRenderer playerSprite = GetComponent<SpriteRenderer>();
playerTransform = transform;
playerBody = GetComponent<Rigidbody2D>();
playerWidth = playerSprite.bounds.extents.x;
playerHeight = playerSprite.bounds.extents.y;
playerAnimController = gameObject.GetComponent<AnimatorController>();
objectHandler = gameMaster.gameObject.GetComponent<GameObjectHandler>();
GameObject dirtScriptObject = GameObject.FindGameObjectWithTag("Dirt");
dirtScript = dirtScriptObject.GetComponent<DiggableDirtScript>();
}
void Update()
{
Vector2 lineCastDown = playerTransform.position.toVector2() - playerTransform.up.toVector2() * playerHeight;
bool interact = Input.GetKeyDown(KeyCode.E);
if (interact)
{
//Interact();
//CheckInFront();
//print("E pressed");
}
//Sets void move float value to that of the GetAxisRaw value determined by the players input
Move(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
//Sets the facing direction values based on the horizontal and vertical input value
if (Input.GetAxisRaw("Horizontal") == 1)
{
facingUp = false;
facingRight = true;
facingDown = false;
facingLeft = false;
}
else if (Input.GetAxisRaw("Horizontal") == -1)
{
facingUp = false;
facingRight = false;
facingDown = false;
facingLeft = true;
}
if (Input.GetAxisRaw("Vertical") == 1)
{
facingUp = true;
facingRight = false;
facingDown = false;
facingLeft = false;
}
else if (Input.GetAxisRaw("Vertical") == -1)
{
facingUp = false;
facingRight = false;
facingDown = true;
facingLeft = false;
}
//Hold shift to run function
if (Input.GetKey(KeyCode.LeftShift))
{
playerSpeed = 3f;
}
else
{
playerSpeed = 1.5f;
}
if (facingDown == true && facingLeft == false && facingRight == false && facingUp == false && Input.GetKeyDown(KeyCode.E))
{
RaycastHit2D hitDown = Physics2D.Raycast(lineCastDown, lineCastDown - playerTransform.up.toVector2() - Vector2.up, 0.05f, playerMask);
if (hitDown.collider != null)
{
if(hitDown.collider.gameObject.CompareTag("Dirt"))
{
if (dirtScript.hasBeenDug == false)
{
bool digUp = true;
dirtScript.DugUp(digUp);
Debug.Log("object name" + hitDown.collider.name + hitDown.collider.tag);
}
}
}
else if (hitDown.collider == null)
{
print("It is null");
}