Translation js to C # : just one line in file
I'm actually working to translate a unity project originally in unityscript/javascript to C#. I already have translated a good part of the project, but i'm confronted to one problem :
In javascript, I have a file named KillParticle.js and in this one there is just one line :
Destroy ( this.gameObject, 1.0);
But if I'm doing the same in C#, Its throwing me an error. How to deal with that in C#? I need to implement this line in Update or Start function ?
Thanks per advance!
PokeRwOw
EDIT :
My KillParticle.cs :
using UnityEngine;
using System.Collections;
public class KillParticle : MonoBehaviour {
Destroy ( this.gameObject, 1.0f);
}
Return : The method must have a return type. If I insert the Destroy in a function Start or Update it working but I need to implement the code in which function ?
Answer by Xarbrough · Sep 20, 2015 at 08:01 AM
You really should supply us with the full error message, but I assume the compiler doesn't like you using a double instead of a float: Use 1.0f to indicate a float value in c#. (1.0 is a double - a number with twice the precision of a standard float). Also, you actually need to call Destroy() from within a method.
public class KillParticle : MonoBehaviour
{
private void Start()
{
Destroy(gameObject, 1.0f);
}
}
Hey ! Thanks for your answer :) But I already did this in my code, I have edited with the error returned.
Sorry, I totally missed, that you missed to wrap your Destroy() call in a method. Unity needs to know when to execute the Destroy call, so you can place it either in Awake, Start or somewhere in the Update loop for example. Edited my previous answer. Awake is called first, then Start right before the first frame of the game is renderer. Update is a loop which is called every frame. So if you want to destroy your GameObject 1 Second after it is created, you can call it in Start.
I just want to Destroy the gameobject, when I fire the file $$anonymous$$illParticle
Answer by Runalotski · Sep 21, 2015 at 11:22 AM
in c# you need to say what the return type of the function is so it would be
public void death()
{
Destroy(gameObject, 1.0f);
}
where void is the return type wich means it returns nothing where you can have differnt return typs like int
private int addNumber(int a, int b)
{
int x = a + b;
return x;
}
I think you can use any type as a return like arrays and custom classes
but if you just want a function to do something then using void is what you want
Sorry i was ment to post this as a comment to PokePoWo's Edit but i clicked the wrong place.
Your answer
Follow this Question
Related Questions
Using foreach to remove and delete bullet in List - C# 3 Answers
Enemies/GameObject disappearing after initial animation 0 Answers
if the ball hits the coin the coin need to be destoyed but how? i have this but it does not work. 2 Answers
I "m having some issues . How do I get the don't destroy on load to work in C# ? 0 Answers
Static Gameobjects Destroy on Reload 0 Answers