Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges