How to designate a specific prefab for the script. (Or how to move something to a specific prefab)
I'm trying to put an instantiate prefab onto another prefab of other type of game object. But I don't know how to tell the script to choose a "specific" prefab for the data/action. Here is my code:
private void PutDown()
{
if (playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Placeable")) && itemHeld && !itemInWay)
{
gameObject.transform.position = placeMat.transform.position; //Put on prefab's location/center
itemHeld = false; //code for something else
playerObject.GetComponent<PlayerScript>().itemInHand = false; //code for something else
}
}
For reference, the character is making coffee(instantiate prefab) and placing it on one of three placemats(prefabs). This Script is "on" the coffee. The public GameObject placeMat is filled with the base prefab of the place mat from the prefab folder.
This code works to move the game object to the prefab's location, but it's the saved location of the very first prefab, not the location of whatever prefab I want to interact with. How do I do specify in the code to interact or reference a specific copy of a prefab (instantiate or otherwise)?
how are you choosing between the placemats currently? do you want to use the respective locations of each of the placemat prefabs or do you want to use the location of the instantiated object?
I think that's the problem. I'm not sure how to tell the script to "choose" between the placemats other than doing IsTouchingLayer or OnTriggerEnter kind of stuff but that doesn't really force the interaction with the "selected placemat". I want to use the placmat prefabs' locations. Also somehow I didn't see your reply until now :/
Answer by yoj346 · Sep 29, 2021 at 12:13 AM
Okay I managed to figure it out! I didn't know this but on something like OnTriggerEnter (or the similar ones to this I assume) when you use the "other" interface, you can do quite a bit with it.
void OnTriggerEnter2D(Collider2D other)
if (other.gameObject.tag == "PlaceMat")
{
placeMatPoint = other.gameObject.transform.position;
}
}
private void PutDown()
{
gameObject.transform.position = placeMatPoint;
}
Basically the place mat gameobject has the "PlaceMat" tag and is triggered in the players "hands" collider so its designated as the "other", allowing me identify it over the other prefabs not triggering the trigger. Now I just needed save a vector2 (placeMatPoint) of the place mats position and then I could use it for dropping the item onto!
I'm still new to all of this but I'm so glad I figured this out. It only too me a month and a half, lol.
so "other" in this case should actually just be the name your assigning to the parameter which will be filled by whatever collider is triggering the object you have this script on. if you replaced all instances of the word "other" with "nonsense", for instance, it should still work. otherwise congrats! this code looks a lot cleaner and more readable.
if you want to immediately trigger putDown and dont need the position for something else, you can even just skip the global variable and make the position vector a parameter and pass it directly :
void OnTriggerEnter2D(Collider2D thingInQuestion)
if (thingInQuestion.gameObject.tag == "PlaceMat")
{
PutDown(thingInQuestion.gameObject.transform.position);
}
}
private void PutDown(Vector2 placeMatPoint)
{
gameObject.transform.position = placeMatPoint;
}
The "other" na$$anonymous$$g thing is something I figured out as well, where its more of a placeholder name that can be change to whatever as you said.
I still don't real understand the whole filling in the () in a method. From what it looks like you're saying in private void PutDown(Vector2 placeMatPoint) that if placeMatPoint become defined, the method will activate its self? Am I understand that right?
so when you define a function or method(which is just a function that belongs to an object) you can define parameters that it will accept and use as arguments within the function
for instance, if you want a function that squares a number you might write
float squarer (float numToSquare) {
return numToSquare * numToSquare;
}
the float before squarer declares that the function returns a float. the float inside the parentheses declares that the function should be provided a float which will be referred to by the numToSquare name that follows within the scope of the function. with this definition we have to call squarer like this: squarer(4) or otherwise(i.e. squarer() or squarer(4,4) we will get an error about the arity of the arguments, which means we provided more or less arguments than the number of parameters which the function is declared to take(any exception you come across is an example of overloading which allows us to define different "versions" of the function that can be called on different parameters as necessary and which automatically use the arguments provided in a given call to deter$$anonymous$$e which version is being referred to). Whats confusing about onTriggerEnter and some of the other built-in methods that you might declare implementations for in a given gameObject, is that you never seem to call onTriggerEnter or provide it with the arguments declared as parameters of the function such as the Collider2d you've named other. this is because this code is handled in the definiton of the gameObject, similarly to the way you define start and update in many scripts, but generally don't call them within that script. this is handled under the hood by all the code that represents the software which makes up the game engine itself.
Your answer
Follow this Question
Related Questions
Why couldn't I use the “Resources.Load” result directly? 1 Answer
Polybrush won't save to prefab 1 Answer
How do I make the same 2d object to fall again and again? 0 Answers
what does the BUILD_ADDITIONAL-folder stands for ? 0 Answers
Editor script: Variable created on gameObject in editor set to missing on runtime 0 Answers