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 /
avatar image
0
Question by p14vais · Apr 06, 2020 at 10:06 AM · spawnprefabspickupdropdownspawning problems

Pickup & Drop Objects + Spawn items HELP

Hello

I have multiple problems with my scripts.

Let me explain you what I want to do:

  • I have some prefabs from the Free Food Pack that I want them to be spawned when you enter a specific location. I want to spawn only one item at a time and not spawn another until i take the item from the spawnpoint no matter i enter the location. I managed to do it but Instead of spawning an item at a time there are 2 items spawning at a time and when i reenter the location there spawn another two items.

    • I want to pick up and drop down an item and i want to be able to hold one item at a time (I have like 10 prefabs).

    I managed to do it but i have some problems with it

    • When an item is spawned I want to pass a script to it so that I can pass the parameters that are associated with the pickable script (pick up drop down items)

    I managed to do it but I have some problems with it

Triggerspawn Script

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class TriggerSpawn : MonoBehaviour {

 public Transform Spawnpoint;
 public GameObject Prefab;
 private bool spawned = false;
 private GameObject PrefabInstance;
 private GameObject TempParent;
 private GameObject breaditem;
 private Pickable pickable;

 void Update()
 {
     pickable = GameObject.FindObjectOfType<Pickable>();
 }

 // Use this for initialization
 void OnTriggerEnter () {
     if (spawned == false) {
         Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation);
         PrefabInstance = Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation);
         PrefabInstance.AddComponent<Pickable>();
         TempParent = GameObject.Find("Player1/Destination");
         Debug.Log(TempParent);
         breaditem = GameObject.Find("Bread(Clone)");
         Debug.Log(breaditem);
         pickable.item = breaditem;
         pickable.tempParent = TempParent;
         pickable.guide = TempParent.transform;
         spawned = true;
     }
 }

}

Pickable Script

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Pickable : MonoBehaviour {

 public GameObject item;
 public GameObject tempParent;
 public Transform guide;
 bool carrying;
 public float range = 5;

 // Use this for initialization
 //void Start()
 //{
 //    item.GetComponent<Rigidbody>().useGravity = true;
 //}
 // Update is called once per frame
 void Update()
 {
     if (carrying == false)
     {
         if (Input.GetKeyDown(KeyCode.K) && (guide.transform.position - transform.position).sqrMagnitude < range / (15 * range))
         {
             pickup();
             carrying = true;
         }
     }
     else if (carrying == true)
     {
         if (Input.GetKeyDown(KeyCode.K))
         {
             drop();
             carrying = false;
         }
     }
 }
 void pickup()
 {
     item.GetComponent<Rigidbody>().useGravity = false;
     item.GetComponent<Rigidbody>().isKinematic = true;
     item.transform.position = guide.transform.position;
     item.transform.rotation = guide.transform.rotation;
     item.transform.parent = tempParent.transform;
 }
 void drop()
 {
     item.GetComponent<Rigidbody>().useGravity = true;
     item.GetComponent<Rigidbody>().isKinematic = false;
     item.transform.parent = null;
     item.transform.position = guide.transform.position;
 }

}

[1]: /storage/temp/155857-1.png

[2]: /storage/temp/155857-2.png

So what are the problems practically:

  • First I enter the point that makes the item spawn and three things appear on the console (1.PNG) the parameters are not passed to the Pickable Script and two prefabs appear

  • Then I reenter the spawnpoint and those three things reappear and there spawn two more prefabs with one of those 4 appeared that passes the parameters to the Pickable Script and I can pick it up

It is the first time I upload a question to unity forum and I am new to Unity. So please don't judge me if I made a newbie mistakes and write a comment below If i can provide you with more information or something I forgot to mention in my Post

Thank You

2.png (326.4 kB)
1.png (391.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

123 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 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

How do I make enemies not spawn in the same position? 2 Answers

I need To spawn game objects randomly, but I'm Getting an error in My script, Please help! 1 Answer

Spawn Random Objects on a Sphere Surface 0 Answers

unity chose random gameobject and spawn it. 2 Answers

How to prevent multiple spawns from a single spawn point? 1 Answer


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