- Home /
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
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.
sorry for that... i cleared the code, so that only the 2 voids are left
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:
Have you checked the spelling of your tags are matching your code?
Have you checked the isTrigger checkbox in the inspector?
I'm sure there are others but I can't think of anymore right now...
Good luck
Your answer
![](https://koobas.hobune.stream/wayback/20220613103957im_/https://answers.unity.com/themes/thub/images/avi.jpg)