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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

JS to C# conversion

Discussion in 'Scripting' started by nonabonasonadona, Dec 8, 2014.

  1. nonabonasonadona

    nonabonasonadona

    Joined:
    Dec 8, 2014
    Posts:
    17
    Hi,
    How would I convert:

    Code (JavaScript):
    1. var Card : GameObject = Instantiate (uniqueCard [i/2], Vector3(0,0,0), Quaternion.identity);
    to c#.

    I tried:
    Code (CSharp):
    1. GameObject theCard = new GameObject();
    2.             theCard(uniqueCard [i/2], Vector3(0,0,0), Quaternion.identity);
    And I am getting errors:
    Expression denotes a `type', where a `variable', `value' or `method group' was expected
    and
    Expression denotes a `variable', where a `method group' was expected

    Thanks in advance
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Code (CSharp):
    1.  
    2. public GameObject Card; //In the inspector drag and drop the "card" prefab into this slot.
    3.  
    4. private GameObject crd; //This is just what I do so that later in the script I have a reference to THIS instance of the object.
    5.  
    6. void Start(){ //If you want it to happen at the start.
    7. crd = Instantiate(card, Vector3.zero, Quaternion.identity);
    8. }
    9.  
     
  3. nonabonasonadona

    nonabonasonadona

    Joined:
    Dec 8, 2014
    Posts:
    17
    Hi,

    I did the assignment to Card, but I get these errors:
    "The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments"

    theCard = Instantiate(uniqueCard [i/2], Vector3.zero, Quaternion.identity), what is wrong with my parameters?
    and why can't I do Vector3(0,0,0) or Vector3(0,-0.05f,0)?

    and I am also getting the error:
    cannot convert `UnityEngine.Vector3' expression to type `UnityEngine.Object'
     
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    The invalid arguement is "uniqueCard [i/2]". This is a variable (I'm assuming) and variables cannot have spaces in them. I'm not even sure what the [i/2] is supposed to mean.

    If you want to declare the Vector3 you have to store it as a variable. You can't set it INSIDE the Instantiate arguments. Again, for example:

    Code (CSharp):
    1.  
    2. public GameObject Card; //In the inspector drag and drop the "card" prefab into this slot.
    3.  
    4. private GameObject crd; //This is just what I do so that later in the script I have a reference to THIS instance of the object.
    5. private Vector3 instantiatePoint;
    6.  
    7. void Start(){ //If you want it to happen at the start.
    8. instantiatePoint = new Vector3(0, 0.05, 0);
    9. crd = Instantiate(card, instantiatePoint, Quaternion.identity);
    10. }
     
  5. nonabonasonadona

    nonabonasonadona

    Joined:
    Dec 8, 2014
    Posts:
    17
    Thank you for your help.
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    @DRRosen3 is incorrect, it is possible to directly transfer everything over. i is an integer, uniqueCard is a GameObject array.

    @nonabonasonadona here is the direct translation.
    Code (csharp):
    1. GameObject Card = (GameObject)Instantiate (uniqueCard [i/2], new Vector3 (0, 0, 0), Quaternion.identity);
     
    nonabonasonadona likes this.