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
  4. Dismiss Notice

Question very new to this c#

Discussion in 'Scripting' started by ezpzbakedmacaroni, Nov 27, 2020.

  1. ezpzbakedmacaroni

    ezpzbakedmacaroni

    Joined:
    Nov 27, 2020
    Posts:
    5

    I am trying to do this


    but the options on the component are not the same.
    here is my code

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

    public class EnemySpawner : MonoBehaviour
    {
    public GameObject enemy;
    float randX;
    Vector2 whereToSpawn;
    public float spawnRate = 2f;
    float nextSpawn = 0.0f;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    if (Time.time > nextSpawn)
    {
    nextSpawn = Time.time + spawnRate;
    randX = Random.Range (-8.4f, 8.4f);
    whereToSpawn = new Vector2 (randX, transform.position.y);
    Instantiate(enemy, whereToSpawn, Quanternion.identify);
    }
    }
    }
     
  2. ezpzbakedmacaroni

    ezpzbakedmacaroni

    Joined:
    Nov 27, 2020
    Posts:
    5
  3. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Welcome to the forum!
    Please use Code Tags for code, so it's easier to read - and more likely you will get a reply! (Not everyone is willing to format it for you, like I am.)

    This is what your code would look like with them:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemySpawner : MonoBehaviour
    6. {
    7.     public GameObject enemy;
    8.     float randX;
    9.     Vector2 whereToSpawn;
    10.     public float spawnRate = 2f;
    11.     float nextSpawn = 0.0f;
    12.  
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Time.time > nextSpawn)
    22.         {
    23.             nextSpawn = Time.time + spawnRate;
    24.             randX = Random.Range (-8.4f, 8.4f);
    25.             whereToSpawn = new Vector2 (randX, transform.position.y);
    26.             Instantiate(enemy, whereToSpawn, Quanternion.identify);
    27.         }
    28.     }
    29. }
    Now that it's easier to read, let's see where the problem is:
    You made a spelling error in line 26. Quanternion.identify should be Quanternion.identity

    If that's not it please check the following:
    Do you still have have any errors showing in the Console Window? If so, what do they say?
    Please create a screenshot showing all of them, or copy paste them here.
     
    Last edited: Nov 27, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779