Hey guys.
Like the title say, I want to know if there’s another way to initiate an object.
I am talking about something like myObject obj = new myObject(…) so each time I want to re-use obj, I just have to initiate it again instead of using Instantiate() and having a bunch of clones around.
A little background of the situation: I have a boomerang object that I create each time I click, but if I click 40 times, I’ll have 40 boomerangs and I just want one. My line of thought is that with one instance of the object, I’m able to know where it is, whats the status (if its moving away/towards the player) or if it is active.
Sorry for the English and thanks for your time.
Let me know if you need more information.
I would create a function in the boomerang script that makes it throw, then call that script instead of instantiating a new one each time. so your boomerang class would be :
class Boomerang{
var throwDistance : int;
var throwable : boolean
function Boomerang(){
throwable = true;
}
function Throw(throwDistance : int){
if(throwable){
whatever script you have that makes it move;
}
}
}
Then in your script attached to the player call it like this:
var boomerang : Boomerang;
function Start(){
boomerang = new Boomerang();
}
function Update(){
if(Input.GetButtonDown("Throw")){
boomerang.Throw();
}
}
and make all your other stuff that returns the boomerang to origin point and turn the throwable boolean off when its flying and back on when it resets.
Instantiate is a CPU intensive call. It would be great if you reuse your instantiated objects. When u have used it set it to inactive by using SetActive on the Game Object.
Simply this way, you cannot throw a new one and you are not instantiating anything at runtime. Your boomerang is already there or created from a game manager class once in the Awake or Start.
Now you must have a Catch function that should reset notFlying = true; so that you are now able to throw again your boomerang.