• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Swains · Jun 14, 2016 at 02:18 AM · instantiateprefabinstantiate prefab

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");
             }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 
     }
 }
 


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Im having an issue when Instantiate Prefab with specific rotation 2 Answers

Prefabs not instantiating after build 0 Answers

I need help destroying a clone on function. 1 Answer

how can I instantiate bullet prefabs continuously while holding the left mouse button? ,how can I instantiate bullet prefabs continuously while holding the left click button? 0 Answers

All instantiated objects having same location, 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges