Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing lightmap index of objects

Discussion in 'Global Illumination' started by flapyfox, May 5, 2015.

  1. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
  2. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    any ideas?
     
  3. Deleted User

    Deleted User

    Guest

    You get the rendering component of the gameObject and then just set it to whatever you want, for example:
    Code (csharp):
    1. MeshRenderer rend = GetComponent<MeshRenderer>();
    2. rend.lightmapIndex = 2;
    I've found it hit and miss though in Unity 5 as re-baking would usually mess it up for me (obviously), but ah well, that's how you'd do it anyway.
     
  4. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    Could you describe the steps?
    I created a Csharp script and attached to the object, it didn't work?
     
  5. Deleted User

    Deleted User

    Guest

    Are you putting it in Awake()? Apparently doesn't work if you put it in there (I wasn't aware of this, I always had it in Start() though)
     
  6. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    Here is what I am writing:

    using UnityEngine;
    using System.Collections;

    public class NewBehaviourScript1 : MonoBehaviour {

    // Use this for initialization
    void Start () {
    MeshRenderer rend = GetComponent<MeshRenderer>();
    rend.lightmapIndex = 2;

    }

    // Update is called once per frame
    void Update () {

    }
    }
     
  7. Deleted User

    Deleted User

    Guest

    Are there any error messages in the Console log? Here's some slightly different code. After you've updated the code, inside the inspector drag and drop the MeshRenderer component into the slot of the script that says "rend" that you want to change the lightmap index of. Also note it can only be changed in Baked and Mixed modes, although going by your screenshot I assume that's the case due to the value of lightmapindex for baked.

    If this doesn't work then I honestly don't know, it's the classic excuse of "well it's working on mine" so starting to run out of answers. Although, you're not just adding the script and checking to see if it has worked right? Because this would change it when you run the game. If you want it to change it inside the editor without playing then that'd require extra code...

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript1 : MonoBehaviour
    5. {
    6.     public MeshRenderer rend = null;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         if (rend != null)
    12.         {
    13.             rend.lightmapIndex = 2;
    14.         }
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.     }
    20. }
     
  8. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    I get this error message when I drag the script to the objects.
    "couldn't add the script because the script class could not be found...
    Yes, I really need this to work in the editor window.
     
  9. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
    I am also having the same issue, all I need is to manually set the lightmap index of an object. Please tell me that somebody found a solution?
     
  10. Jde

    Jde

    Joined:
    Oct 2, 2011
    Posts:
    139
  11. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
  12. samuraijhonson

    samuraijhonson

    Joined:
    Jul 28, 2015
    Posts:
    10
  13. WagDan

    WagDan

    Joined:
    Nov 7, 2014
    Posts:
    37
    Anyone solved this?

    Have assigned the lightmaps using the SetLightmapsManually.cs but this info is lost on play and on build.