Avisek Chakraborty
Avisek Chakraborty

Reputation: 8309

Get sprite prefab width

I have a Prefab which is basically a sprite. It has a javascript attached & I want to get width of it.

Here is what my assumption is-

private var getObjectWidth:float;

function start(){
    // want to get the gameObject's spriteRenderer 
    var gameObjectRenderer = transform.gameObject.GetComponent(SpriteRenderer);

    // then get the width from Sprite bounds
    getObjectWidth = gameObjectRenderer.sprite.bounds.size.x;
}

Upvotes: 1

Views: 3723

Answers (1)

user3183542
user3183542

Reputation: 196

http://docs.unity3d.com/ScriptReference/Renderer-bounds.html

check that link out. There is no sprite variable for Renderer

I think you could use

gameObjectRenderer.bounds.max.x - gameObjectRenderer.bounds.min.x;

or more simply

gameObjectRenderer.bounds.extents;

check this to

http://docs.unity3d.com/ScriptReference/Bounds.html

Upvotes: 4

Related Questions