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 /
This question was closed Feb 08, 2015 at 11:15 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Biggidybigz · Feb 07, 2015 at 06:15 PM · c#instantiatevariablegetcomponentpass

How to call a function on an instance

On my playerScript, when i press spacebar, the function "PlantBomb" is called. This instantiates the "bomb" prefab. I'd like to run a function of bombScript (the script of bomb), and pass it a variable, letting it know how long to wait before exploding. The only error i'm getting, is the line that has bombTimer on it. It's bombTimer is red, like it doesn't know what the function is. This function exists on the bombScript though. Any help?

 public GameObject bomb;
 
 void PlantBomb () {
 var bombClone = (GameObject)Instantiate (bomb, transform.position, transform.rotation) as GameObject;
 bombClone.GetComponent<bombScript>.bombTimer (5);
 }
 
 if (Input.GetKeyDown(KeyCode.Space))
 {
     PlantBomb ();
 }
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 getyour411 · Feb 07, 2015 at 07:13 PM 0
Share

Is this C# or UnityScript? Is that second block in Update() or what? Functions often have uppercase names are you sure it's bombTimer function and not BombTimer?

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by Mmmpies · Feb 07, 2015 at 07:25 PM

If C# try this:

 bombClone.GetComponent<bombScript>().bombTimer (5);
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 Biggidybigz · Feb 08, 2015 at 03:08 AM 1
Share

Thanks mmpies, that was it! Also i realised i needed to make the bombTimer function public in the bombScript.

avatar image Mmmpies · Feb 08, 2015 at 10:17 AM 0
Share

No problem @Biggidybigz see this is your first question, if it's the right answer it's a good idea to tick it correct, it shows people looking for the same thing that it's answered, stops others from looking at it to fix, gives me some karma points and means anyone looking at your profile that you tick the answers. If you get a reputation for not ticking the correct answer then some people don't bother looking at your questions.

You can even tick your own answer if you find a fix yourself. Best practice is tick the best or first answer if all similar. e.g. One of my questions no one knew the answer, I worked out a work around and ticked my own fix. Couple of weeks later someone came up with a much better way so I un-ticked my answer and ticked theirs.

Hey glad it worked though.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Creating an ordered list from a variable 1 Answer

Loading a Constant Array Of Integers 0 Answers

Problem calling variable across scripts (Closed) 1 Answer

Referencing in a prefab 1 Answer

How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 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