- Home /
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612230543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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