Question perfomance

Hi. I would like to know if anyone knows which of these 2 is the appropriate way to turn off object :

Bullet = Instantiate(Resources.Load (“Ammo/Bullet”))as Transform;

if(Bullet.gameObject.activeSelf == false)
{
Bullet*.position = SpawnPoint.position;*
Bullet*.rotation= SpawnPoint.rotation;*
Bullet*.gameObject.SetActive(true);*
return;
}
--------------------------------------------------------------------------------------------------------
Bullet = Instantiate(Resources.Load (“Ammo/Bullet”))as GameObject;
if(Bullet.activeSelf == false)
{
Bullet.transform*.position = SpawnPoint.position;*
Bullet.transform*.rotation= SpawnPoint.rotation;*
Bullet*.SetActive(true);*
return;
}

In that script you’re actually turning it on. In any case, first one is better.
And you’re doing a few things wrong to start with:

  1. You shouldn’t write variables with a capital first letter. So “Bullet” should be “bullet”, and “SpawnPoint” should be “spawnPoint”.
  2. Don’t load them from resource, is way slower, instead store the object in a variable, drag and drop it into the inspector, and instantiate it from the variable.
  3. I don’t think you need the “return” line.

You should watch some getting started tutorials, start with Unity’s official ones.

Dont listen to him. Write yhour public variables with capital, privates with non-capital. Thats the general way of doing it.

No its not. You can see that in 99.95% of scripts you encounter. Class names should be first letter capital, and variables first letter lower case. Its general guidelines and allows consistency among code and between developers, while avoiding problems. Take a look at the Unity’s scripting reference page for instance. Its all lower case for variables. Unless you’re saying people who made Unity are doing it all wrong. :stuck_out_tongue:

Umm, no. Fields are camal case according to the c# conventions.

It’s just not true.
private fields are pascal case, public camelcase.
properties are camelcase.

The first one will probably be a bit faster simply because you’re not accessing GameObject.Transform multiple times.

Meh, I stand corrected by Microsoft. However, I tend to see a lot more people suggest otherwise. Even the entire documentation of c# uses other conventions.

However, anyone can use either. Sorry to get off topic.

thanks for the reply, just to clarify:

Bullet = Instantiate(Resources.Load (“Ammo/Bullet”))as Transform; <— are inside Awake()

Both ways are possible. I would use the second one though (as gameobject).