Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MadsWiering · Dec 02, 2014 at 03:37 PM · instantiatespritescreentoworldpoint

[Not Solved]Instintiated gameobject(sprite) wont load on scene

Hello,

In my code i am instantiaing a gameobject with a sprite multiple times. But only the original gameobject will load the sprite at the correct place. but the others ones wont.

(Im still a newbie so code wont look to nice)

edit: So i made a few changes, but i still have the same problem even though the objects arent in the same place anymore.

Instantiate code:

 void Start () 
 {
     PlaceBlocks();
 }


 void PlaceBlocks()
 {
     for( int i = 0 ; i <= 6 ; i++)
     {
         GameObject _go = (GameObject)Instantiate( _letterBlock ) as GameObject;
         _go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3((_blockPosX[0]),(_blockPosY[i]),-2));
         this._letterBlockList.Add(_go);
     }

     for( int i = 0 ; i <= 5 ; i++)
     {
         GameObject _go = (GameObject)Instantiate( _letterBlock ) as GameObject;
         _go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3((_blockPosX[1]),(_blockPosY[i]),-2));
         this._letterBlockList.Add(_go);
     }
 }

}

Instantiated object code:

 void Start () 
 {
     
     CreateButtons();
     
 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetKeyUp(KeyCode.A)){
         CreateButtons();
         print ("aaaargh");

     }
 }
 
 void CreateButtons()
 {
     
     string _textPath = "Assets/Texture/button 3.png";
     Texture2D _buttonText = (Texture2D)Resources.LoadAssetAtPath(_textPath, typeof(Texture2D));
     
     SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
     Sprite sprite = new Sprite();
     sprite = Sprite.Create( _buttonText, new Rect(0,0,400,400), new Vector2 (0.5f,0.5f),250.0f);
     renderer.sprite = sprite;
     renderer.material = _dfltMaterial;
     
     
 }

 void OnMouseDrag()
 {
     _moveObject.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
     _moveObject.y = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
     _moveObject.z = -2;

     transform.position = _moveObject;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jenci1990 · Dec 02, 2014 at 06:24 PM

  _go.transform.position = Camera.main.ScreenToWorldPoint(_position);

the "_position" variable never change, so all gameObject are at the same position.

 for( int i = 0 ; i <= 6 ; i++) {
     GameObject _go = (GameObject)Instantiate( _letterBlock ) as GameObject;
     _go.transform.position = Camera.main.ScreenToWorldPoint(_position);
     this._letterBlockList.Add(_go);
     //Change the postition
     _position.x += 1f;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadsWiering · Dec 02, 2014 at 06:41 PM 0
Share

$$anonymous$$ade changes but still same problem occurs, and i checked that they arent in the same places anymore.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiate Prefab 1 Answer

Problems with ScreenToWorldPoint in 2D game 1 Answer

Camera ScreenToWorldPoint Instatiating 1 Answer

Instantiated Sprite is not rendering. 1 Answer

Rotating a sprite toward the mouse position on instantiation. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges