Skip to main content
Formatting & tagging correctly, clarifying
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

problem of dynamic declaration Moving a dynamically spawned object

iI would like to create an object dynamically when the player presses Q and use itsthis object in InstantiateInstantiate and in transform.Translate but i can't !

Blockquote using System.Collections;

using System.Collectionstransform.Translate.Generic;

using UnityEngine;

public class instancier_new_objectThe problem is that the translation must be outside the : MonoBehaviourif that checks for the {Q press, but it applies to the cube that I created inside the if.

public floatusing moveSpeed=5f;System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Use thispublic forclass initialization
voidinstancier_new_object Start: ()MonoBehaviour { 

    
}

//public Updatefloat ismoveSpeed=5f;
 called once per frame
    void Update () {
 
        if (Input.GetKeyDown (KeyCode.Q)) {
            GameObject objet = new GameObject ("objet");
             
            GameObject cube = new GameObject ("cube");

        cube=Instantiate    cube = Instantiate(
                       objet,
                       new Vector3 (1, 2, 3),
                       Quaternion.identity);

    }
         );
        } 
          

// the problem is that the translation must be outside the if while only applies to the cube that can only be created in if

   
        cube.transform.Translate(
                 Time.deltaTime*moveSpeed,
                 0,
                -Time.deltaTime*moveSpeed,
                 Space.Self
        );
    }
}

}

Can you correct me?

thanks

problem of dynamic declaration object

i would like to create object dynamically and use its in Instantiate and in transform.Translate but i can't !

Blockquote using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class instancier_new_object : MonoBehaviour {

public float moveSpeed=5f;



// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
 
    if (Input.GetKeyDown (KeyCode.Q)) {
        GameObject objet = new GameObject ("objet");
         
        GameObject cube = new GameObject ("cube");

        cube=Instantiate (objet, new Vector3 (1, 2, 3), Quaternion.identity);

    }
         
         
          

// the problem is that the translation must be outside the if while only applies to the cube that can only be created in if

         cube.transform.Translate(Time.deltaTime*moveSpeed,0,-Time.deltaTime*moveSpeed,Space.Self);
    
}

}

Can you correct me?

thanks

Moving a dynamically spawned object

I would like to create an object dynamically when the player presses Q and use this object in Instantiate and in transform.Translate.

The problem is that the translation must be outside the if that checks for the Q press, but it applies to the cube that I created inside the if.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class instancier_new_object : MonoBehaviour { 

    public float moveSpeed=5f;
    
    void Update () {
        if (Input.GetKeyDown (KeyCode.Q)) {
            GameObject objet = new GameObject ("objet");            
            GameObject cube = new GameObject ("cube");

            cube = Instantiate(
                       objet,
                       new Vector3(1, 2, 3),
                       Quaternion.identity
            );
        } 
             
        cube.transform.Translate(
                 Time.deltaTime*moveSpeed,
                 0,
                -Time.deltaTime*moveSpeed,
                 Space.Self
        );
    }
}
deleted 51 characters in body
Source Link

i would like to create object dynamically and use its in Instantiate and in transform.Translate but i can't !

Blockquote using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class instancier_new_object : MonoBehaviour {

public float moveSpeed=5f;



// Use this for initialization
void Start () {
    
}

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

    if (Input.GetKeyDown (KeyCode.Q)) {
        GameObject objet = new GameObject ("objet");
        //objet.transform.position = new Vector3 (1, 1, 1);
        GameObject cube = new GameObject ("cube");

        cube=Instantiate (objet, new Vector3 (1, 12, 13), Quaternion.identity);

    }
         
         
          

// the problem is that the translation must be outside the if while only applies to the cube that can only be created in if

         cube.transform.Translate(Time.deltaTime*moveSpeed,0,-Time.deltaTime*moveSpeed,Space.Self);
    
}

}

Can you correct me?

thanks

i would like to create object dynamically and use its in Instantiate and in transform.Translate but i can't !

Blockquote using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class instancier_new_object : MonoBehaviour {

public float moveSpeed=5f;



// Use this for initialization
void Start () {
    
}

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

    if (Input.GetKeyDown (KeyCode.Q)) {
        GameObject objet = new GameObject ("objet");
        //objet.transform.position = new Vector3 (1, 1, 1);
        GameObject cube = new GameObject ("cube");

        cube=Instantiate (objet, new Vector3 (1, 1, 1), Quaternion.identity);

    }
         
         
          

// the problem is that the translation must be outside the if while only applies to the cube that can only be created in if

         cube.transform.Translate(Time.deltaTime*moveSpeed,0,-Time.deltaTime*moveSpeed,Space.Self);
    
}

}

Can you correct me?

thanks

i would like to create object dynamically and use its in Instantiate and in transform.Translate but i can't !

Blockquote using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class instancier_new_object : MonoBehaviour {

public float moveSpeed=5f;



// Use this for initialization
void Start () {
    
}

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

    if (Input.GetKeyDown (KeyCode.Q)) {
        GameObject objet = new GameObject ("objet");
        
        GameObject cube = new GameObject ("cube");

        cube=Instantiate (objet, new Vector3 (1, 2, 3), Quaternion.identity);

    }
         
         
          

// the problem is that the translation must be outside the if while only applies to the cube that can only be created in if

         cube.transform.Translate(Time.deltaTime*moveSpeed,0,-Time.deltaTime*moveSpeed,Space.Self);
    
}

}

Can you correct me?

thanks

Source Link

problem of dynamic declaration object

i would like to create object dynamically and use its in Instantiate and in transform.Translate but i can't !

Blockquote using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class instancier_new_object : MonoBehaviour {

public float moveSpeed=5f;



// Use this for initialization
void Start () {
    
}

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

    if (Input.GetKeyDown (KeyCode.Q)) {
        GameObject objet = new GameObject ("objet");
        //objet.transform.position = new Vector3 (1, 1, 1);
        GameObject cube = new GameObject ("cube");

        cube=Instantiate (objet, new Vector3 (1, 1, 1), Quaternion.identity);

    }
         
         
          

// the problem is that the translation must be outside the if while only applies to the cube that can only be created in if

         cube.transform.Translate(Time.deltaTime*moveSpeed,0,-Time.deltaTime*moveSpeed,Space.Self);
    
}

}

Can you correct me?

thanks