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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

GameObject transform position not working

Discussion in 'Scripting' started by hussamadin, Jul 15, 2014.

  1. hussamadin

    hussamadin

    Joined:
    May 5, 2014
    Posts:
    1
    Hi,
    I'm completely new to unity, with flash as3 background.
    I'm trying to load images from web let say book covers each when user click on any one one of the a new scene should be open.
    It was easy to do in flash but I'm stick with for about 2 weeks searched the internet for solution and find none.
    I have two c# classes on called shelf which should download the images and the other called bookCover where it load the assigned image to sprite render. I managed to load them but I can't set their postions, the gameObject transform position doesn't seems to work. please help it's very critical thank you.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using SimpleJSON;
    4. public class Shelf : MonoBehaviour {
    5.     public float shelfStartPointX = 500.10f;
    6.     public float shelfStartPointY = 40.1f;
    7.     private int page ;//= Math.ceil(dataOfUserBook['response'].length/12);
    8.     private int count = 0;
    9.     private int colOffset =(int) 390/3;
    10.     private int rowOffset= (int) 600/4;
    11.     private int  pageOffset = 530;
    12.     private int jVal = 0;
    13.     private int iVal = 0;
    14.     private int kVal = 0;
    15.     private string url="http://www.kitabwakanz.com/api/v2/index.php/book/getBooks";
    16.     private WWW w;
    17.     private JSONNode json;
    18.     private GameObject selectorSprite;
    19.     private bool loading=false;
    20.     void Start () {
    21.         //w = new WWW(url);
    22.         //yield return w;
    23.         StartCoroutine (getBooksCovers ());
    24.             //    addBooksCovers ();
    25.     }
    26.     IEnumerator getBooksCovers()
    27.     {
    28.         WWWForm req = new WWWForm ();
    29.         req.AddField ("activeSalt", GameStatics.accessToken);
    30.  
    31.         WWW w = new WWW (url, req);
    32.         loading = true;
    33.         Debug.Log ("waitng Response...");
    34.         yield return w;
    35.  
    36.         if (!string.IsNullOrEmpty (w.error)) {
    37.      
    38.                     Debug.Log ("error");
    39.         } else {
    40.             string response = w.text;
    41.             Debug.Log(response);
    42.             json = JSON.Parse(response);
    43.             Debug.Log(json["status"]);
    44.             if(json["status"].AsInt==1){
    45.                 //Debug.Log("inside true " + json["response"][0]["id"]);
    46.                 loading = false;
    47.                 addBooksCovers();
    48.             }
    49.         }
    50.  
    51.     }
    52.     void Update(){
    53.         if(Input.GetMouseButtonDown(0)) {
    54.             print("at Get Mouse down ");
    55.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    56.         //    print("hit is " + hit.collider.gameObject.name);
    57.             if(hit.collider != null) {
    58.                 print("hit is " + hit.collider.gameObject.name);
    59.                 if( hit.collider.gameObject.name == "") {
    60.                  
    61.                 }
    62.             }
    63.         }
    64.     }
    65.     void addBooksCovers(){
    66.         Debug.Log ("at addCovers " + json["response"].Count);
    67.  
    68.         page =(Mathf.CeilToInt (json["response"].Count /12))+1 ;
    69.         Debug.Log ("page" + page);
    70.         for (int j=0; j<page; j++) {
    71.             for(int i=0;(i<3 && count<json["response"].Count );i++){
    72.                 for(int k=0;(k<4 && count<json["response"].Count);k++){
    73.                     Debug.Log("inside for");
    74.                     float xx =shelfStartPointX - (pageOffset * jVal + colOffset * kVal);
    75.                     float yy =shelfStartPointY +(rowOffset *  iVal);
    76.                     GameObject go = new GameObject();
    77.                     go.isStatic = false;
    78.                     go.transform.localPosition = new Vector2(20,0);
    79.                     Debug.Log(go.transform.position);
    80.                     BookCover bc = go.AddComponent<BookCover>();
    81.                     //bc.transform.position = new Vector3(xx,yy,0);
    82.                     go.AddComponent<SpriteRenderer>();
    83.                     //go.AddComponent<BoxCollider2D>();
    84.                     BoxCollider2D boxCollider =  go.AddComponent<BoxCollider2D>();
    85.                     boxCollider.transform.position = new Vector2(0,0);
    86.                     boxCollider.size = new Vector2(100,130);
    87.                     boxCollider.isTrigger =true;
    88.                    
    89.                  
    90.              
    91. //                    bc.transform.position = new Vector2(xx,yy);
    92.              
    93.                     if(json["response"][count]["assigned"].AsInt ==1){
    94.                         bc.setCover(json["response"][count]["id"].AsInt,json["response"][count]["title"],json["response"][count]["preReqId"].AsInt,json["response"][count]["coverImage"],json["response"][count]["unlockLevel"].AsInt,json["response"][count]["level"].AsInt,json["response"][count]["assigned"].AsInt,json["response"][count]["bonusGemOnCompletion"].AsInt,json["response"][count]["deadLine"],json["response"][count]["overDue"].AsInt);
    95.                     }else{
    96.                         bc.setCover(json["response"][count]["id"].AsInt,json["response"][count]["title"],json["response"][count]["preReqId"].AsInt,json["response"][count]["coverImage"],json["response"][count]["unlockLevel"].AsInt,json["response"][count]["level"].AsInt,json["response"][count]["assigned"].AsInt,0,"",0);
    97.                     }
    98.  
    99.                     bc.loadCover();
    100.              
    101.                     kVal++;
    102.                     count++;
    103.                 }
    104.                 if (kVal >= 4) {
    105.                     kVal = 0;
    106.                 }
    107.                 iVal++;
    108.             }
    109.             jVal++;
    110.         }
    111.  
    112.     }
    113.  
    114.     void OnGUI()
    115.     {
    116.         if (loading == true) {
    117.        
    118.             GUI.Box(new Rect(( Screen.width - 200)/2, (Screen.height - 60)/2, 200, 60), "Wait...");
    119.         }
    120.     }
    121. }
    122.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. //using System.Drawing;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class BookCover : MonoBehaviour {
    8.     WWW w;
    9.  
    10.     private string coverUrl;
    11.     private int bookId;
    12.     private string bookname;
    13.     private int preReqId;
    14.     //private string imageUrl;
    15.     private int userLevel;
    16.     private int bookLevel;
    17.     private int isAssignment;
    18.     private int assignmentBonus;
    19.     private string deadline;
    20.     private int overDue;
    21.     private Texture2D coverTexture;
    22.     //public Material defaultMaterial;
    23.     //private GUITexture guiTexture ;//= new GUITexture();
    24.     void Start () {
    25.  
    26.  
    27.  
    28.         //coverTexture = new Texture2D (100, 150);
    29.  
    30.  
    31.  
    32.     }
    33.     public void setCover(int id,string name ,int preReq,string url,int userlvl,int level,int isAss, int assign, string dline,int overD){
    34.  
    35.         bookId = id;
    36.         bookname = name;
    37.         preReqId = preReq;
    38.         userLevel = userlvl;
    39.         bookLevel = level;
    40.         isAssignment = isAss;
    41.         assignmentBonus = assign;
    42.         deadline = dline;
    43.         overDue = overD;
    44.         coverUrl = "http://www.kitabwakanz.com/" + url;
    45.  
    46.  
    47.  
    48.  
    49.     }
    50.     public void loadCover(){
    51.  
    52.      
    53.         StartCoroutine (OnloadCover ());
    54.  
    55.  
    56.     }
    57.  
    58.  
    59.     IEnumerator OnloadCover(){
    60.  
    61.         w = new WWW(coverUrl);
    62.         yield return w;
    63.  
    64.         SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
    65.         Sprite sprite = new Sprite();
    66.  
    67.         sprite = Sprite.Create(w.texture, new Rect(0,0, 100, 130),new Vector2(0, 0),100.0f);
    68.  
    69.         renderer.sprite = sprite;
    70.      
    71.  
    72.     }
    73.  
    74.  
    75. }
    76.  
     
  2. bigcheese_

    bigcheese_

    Joined:
    Sep 6, 2011
    Posts:
    31
    I'm not quite sure what you're trying to do here, but from this:

    Code (CSharp):
    1.   GameObject go = new GameObject();
    2.                     go.isStatic = false;
    3.                     go.transform.localPosition = new Vector2(20,0);
    4.                     Debug.Log(go.transform.position);
    5.                     BookCover bc = go.AddComponent<BookCover>();
    6.                     //bc.transform.position = new Vector3(xx,yy,0);
    7.                     go.AddComponent<SpriteRenderer>();
    8.                     //go.AddComponent<BoxCollider2D>();
    9.                     BoxCollider2D boxCollider =  go.AddComponent<BoxCollider2D>();
    10.                     boxCollider.transform.position = new Vector2(0,0);
    11.                     boxCollider.size = new Vector2(100,130);
    12.                     boxCollider.isTrigger =true;
    I suppose this is where you're trying to position you GameObjects that hold the images? Can you check the scene view and see where they're positioned exactly.

    According to this you're trying to setup the position locally with (20,0) and after that globally by (0,0) ?
    Using vector2 will automatically set up the Z component to 0. which could be the reason it's not rendered on the camera (depending on the camera's position)