How to spawn a prefab at a duplicate objects location
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");
}
Answer by Swains · Jun 14, 2016 at 02:39 AM
Okay I've come up with a solution but not too sure how good it is. Basically just getting the hit objects transform and sending it to the dig up function in the dirt script. Not sure anyone will ever need this help, but still.
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"))
{
Transform dirtArea;
dirtArea = hitDown.collider.gameObject.transform;
bool digUp = true;
dirtScript.DigUpDirt(digUp, dirtArea);
Debug.Log("object name" + hitDown.collider.name + hitDown.collider.tag);
}
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 DigUpDirt(bool dug, Transform spawnTransform)
{
//Destroy(dirtSprite);
//dirtSprite.enabled = false;
Instantiate(dugUpDirtPrefab, spawnTransform.position, Quaternion.identity);
hasBeenDug = dug;
}
}
Your answer
Follow this Question
Related Questions
All instantiated objects having same location, 0 Answers
Get a Insantiate particle system follow a GameObject 1 Answer
How to Change the Variables of an Instantiated Object? 0 Answers
How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers
Im having an issue when Instantiate Prefab with specific rotation 2 Answers