3

I'm instantiating a gameObject from prefab, and I want to set local scale using code, but when I'm setting transform.localScale to the gameObject then it loses material and becomes black.

public GameObject ballObject;    

 public GameObject GetBall(Ball ball)
     {
         GameObject gameObject = Instantiate(ballObject, ball.position, Quaternion.identity);
         gameObject.transform.localScale = ball.scale; // actually, this line destroys the material
         gameObject.tag = Constants.ballTag;

         return gameObject;
     }

enter image description here

enter image description here

Edric
  • 24,639
  • 13
  • 81
  • 91
  • Odd. are you sure its not just because the nose in the middle is black, so it comes black too? – BugFinder Mar 24 '19 at 14:42
  • when you say it loses its material is it actually showing as gone in the inspector?, also is this a model you have imported? if so check the normals on it as the when setting the scale it might be inverting them – akaBase Mar 24 '19 at 18:33
  • Thanks akaBase, that was model issue, I was setting Vector2 instead of Vector3. – Karen Minasyan Mar 25 '19 at 06:45
  • can you please check the value of the scale in ```Debug.Log()``` and make sure that no axis is set to zero in scakl value. – Ghost The Punisher Jan 27 '20 at 07:49

1 Answers1

0

It looks like 'ball.scale' may have a value of zero in one axis, which is flattening the object, which can mess with the appearance in this way.

Check what the scale values are for the incoming object.

FacePuncher7
  • 139
  • 7