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 JulioKoebes · Jun 11, 2013 at 05:16 PM · c#parameterscallvoidpass

calling an void from another script and passing paramters...

Heyho

I have this void in Script A which should destroy previsously spawned gameObjects:

     public void PickedUp(GameObject pickupCollected)
     {
     
     // retrieve name of the collected pickup and cast to int
     int index = int.Parse(pickupCollected.name);
     
     // pickup has been destroyed so make the spawn index available again
     spawnIndexAvailableList[index] = true;
     
     // destroy the pickup
     Destroy(pickupCollected);
     
     // spawn a new pickup
     //SpawnPickup();
     }
     
 }


Now i have Script B which i put on the prefab of the spawned GameObjects. There is this void which shoud call the function PickedUp from ScriptA and pass the pickup that gets hit as the parameter for the void.

     public void OnTriggerEnter(Collider collider)
     {    
     if (collider.gameObject.tag.Equals("Player") && gameObject.tag.Equals("Pointbonus")) 
         {
 
             Counter.Points += 100;
             //Destroy(gameObject);
             picked.PickedUp(gameObject);
             
         }
     if (collider.gameObject.tag.Equals("Player") && gameObject.tag.Equals("Faster")) 
         {
             PlayerMovementController.speed = 8.0f;
                         picked.PickedUp(gameObject);
             //Destroy(gameObject);
         }
     }
     
 }


In void PickedUp the Object is supposed to be destroyed and spawned again after some time(havent done it yet because destroying doesnt work).

There are no compiling error but nothing happens if i hit the object. I tried destroying the object in script B and then it works, but if i do it that way the spawn point never gets resettet... Any ideas...

The orignal code is from an tutorial which was orignally written in JS, where everything worked...

thanks and greetings

edited for more clarity. i though maybe you need the whole code... sorry

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 Jessy · Jun 11, 2013 at 07:04 PM 0
Share

This is too much fluff to wade through. Condense the code to what actually matters. Take some time to make a simple example. You get a -1 for laziness until you put that effort in.

avatar image JulioKoebes · Jun 11, 2013 at 07:17 PM 1
Share

sorry for that... i cleared the code, so that only the 2 voids are left

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SubatomicHero · Jun 14, 2013 at 12:36 PM

OK first thing that I can see if this line:

 // retrieve name of the collected pickup and cast to int
 int index = int.Parse(pickupCollected.name);

Do you know how int parsing works? If the names of your pickups are "1,2,3 etc" its fine but if the names of your pickups do NOT contain any numbers then the assignment to index will throw an error. Read this link regarding int parsing.

Secondly in your IF statements in Script B, you need to add Debug Logs to see if the conditions are being met to compile the rest of the code. And I would simplify the code to look like this:

 public void OnTriggerEnter(Collider collider) {
     if (collider.gameObject.tag == "Player") {
         // OK so we know we have hit a player
         switch(this.gameObject.tag){
         case "Pointbonus":
             Counter.Points += 100;
             picked.PickedUp(this.gameObject);
             break;
         case "Faster":
             PlayerMovementController.speed = 8.0f;
             picked.PickedUp(this.gameObject);
             break;
         }
     }
 }

You also need to do other basic checks:

  1. Have you checked the spelling of your tags are matching your code?

  2. Have you checked the isTrigger checkbox in the inspector?

  3. I'm sure there are others but I can't think of anymore right now...

Good luck

Comment
Add comment · 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

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

16 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

Related Questions

How to Skip Parameter in C# 2 Answers

How can I write: if this method (X) is activated by another specific method (Y) do this (z) 1 Answer

Check if a function is no longer being called? 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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