Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 yoj346 · Aug 14, 2021 at 12:33 AM · prefabprefab-instance

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)?

Comment
Add comment · Show 2
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
avatar image junk_rat_angel · Aug 14, 2021 at 04:12 AM 0
Share

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?

avatar image yoj346 junk_rat_angel · Sep 28, 2021 at 05:08 AM 0
Share

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 :/

1 Reply

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

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.

Comment
Add comment · Show 5 · 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
avatar image junk_rat_angel · Sep 29, 2021 at 07:16 PM 1
Share

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


avatar image yoj346 junk_rat_angel · Sep 29, 2021 at 08:08 PM 0
Share

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?

avatar image junk_rat_angel yoj346 · Sep 29, 2021 at 10:02 PM 1
Share

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.

Show more comments

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

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

Prefab instantiation get position issue 1 Answer

How to instantiate Prefabs for an endless game? 0 Answers

Unity overwrites prefab values in runtime 0 Answers

Is usage of Prefab will help to save memory ? 1 Answer

How to make a prefab a parent to other prefabs? 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