Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question perfomance

Discussion in 'Scripting' started by Tressor_Studio, Jul 18, 2014.

  1. Tressor_Studio

    Tressor_Studio

    Joined:
    May 28, 2013
    Posts:
    45
    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;
    }

     
  2. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    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.
     
  3. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
    Dont listen to him. Write yhour public variables with capital, privates with non-capital. Thats the general way of doing it.
     
  4. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    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. :p
     
  5. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Umm, no. Fields are camal case according to the c# conventions.
     
  6. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The first one will probably be a bit faster simply because you're not accessing GameObject.Transform multiple times.
     
  8. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    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.
     
  9. Tressor_Studio

    Tressor_Studio

    Joined:
    May 28, 2013
    Posts:
    45
    thanks for the reply, just to clarify:

    Bullet = Instantiate(Resources.Load ("Ammo/Bullet"))as Transform; <--- are inside Awake()
     
  10. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
    Both ways are possible. I would use the second one though (as gameobject).