Is Instantiate() the only way to create a new object dynamically?

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.

For Reference

If you object inherits from MonoBehaviour it is not recommended to declare a constructor. So this:

class MyClass :MonoBehaviour{
   public MyClass(){}
}

will mess up internally. This is because Unity has its own way of doing creation of object. So to remedy, they offer the Instantiate function.

Now if you do not need Unity library you can create your own class:

class MyClass{
   public MyClass(){}
}
MyClass object = new MyClass();

and it is fine.

Now for your issue:

bool notFlying;
void Start(){
notFlying = true;
}
void Udpate(){
    if(Input.GetKeyDown(KeyCode.Space)&& notFlying){
       Throw();
       notFlying = false;
    }
}

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.