Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Xelioth · Apr 09, 2014 at 04:52 AM · prefabmultiplayerplayerreferencespell

How do I get a prefab to know who made it?

     var spellOne : Transform;
     var coolDownOne : float;
     var timerOne : float;
 
 function Update () {
     timerOne++;
 
             if (Input.GetButton ("Cast1")) {
                 if(coolDownOne < timerOne) {
                 var instanceBulletOne = Instantiate(spellOne, transform.position, transform.rotation);
                 timerOne = 0;
                 }
                 
             else
             {
                 if(coolDownOne > timerOne) {
                 print("Nope!");
             }
             }
             }


So this is the basis of my spell system. It works beautifully. I press a button I've called Cast1 and it instantiates a prefab that functions as whatever spell I would like. I have one that acts as a fireball, another that works as a wall, and some others I'm toying with. I have a turret and two players using these spells right now, and I would like to use them with enemies once I get to that point as well, ideally.

What I need, though, is the ability for those prefabs to know who cast them. If Player1 casts a homing missile, I want it to be able to check for the nearest enemy and put that enemy into its Target variable. If Player2 casts a Blink spell, I want it to know that Player2 is the one who needs to move forward by a few feet. You get the idea.

How would I go about approaching that?

Comment
Add comment · Show 1
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 thomasindustry · Apr 09, 2014 at 08:52 PM 0
Share

Do you have some sort of script that is attatched to each player? You could have a variable in the spell that holds a reference to that player script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Apr 09, 2014 at 04:57 AM

One thought:

instanceBulletOne.SetInitator(this.gameObject);

on the prefab add a script/method named SetInitiator that accepts a GameObject parameter; you can of course call those whatever you want. The prefab now knows what GameObject called it.

Comment
Add comment · Show 2 · 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 Xelioth · Apr 09, 2014 at 07:13 PM 0
Share

That sounds like a good option, but I'm a little fuzzy on the interaction between the bits of code (I'm new to coding).

So in your suggestion I would have instanceBulletOne.SetInitiator(this.gameObject), which will basically create the prefab and define itself as the person who instantiated that prefab. But on the prefab itself, what would I use to call back to the SetInitiator? How would the prefab know where to look for its mommy?

avatar image getyour411 · Apr 09, 2014 at 09:08 PM 0
Share

The Instantiate line (as you have it) is what creates the instance of the prefab, my line just makes a call to the instance and passes the reference gameobject;

Within the prefab, you could then use the passed reference (as it came into SetInitiator) to do whatever you want.

Ff you just want to set a parent/child relationship you could create that via instanceBulletOne.transform = gameObject.transform (after the Instantiate) though I'm not sure that's what you asked for.

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

22 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

Related Questions

Multiple player prefabs in the network manager? 4 Answers

I have problem with synchronize multiplayer player position... 0 Answers

Multiplayer Implimentation on 2D Character 0 Answers

Multiplayer prefab player fall through floor on instantiate 0 Answers

NetworkBehaviour has an "isLocalPlayer" boolean, but is there a sort of "localPlayer" G.O. reference? 0 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