Having trouble getting the rigidbody2d from the Instantiated objects

I am having an issue on the laser defender course, i have gone out of my way to integrate mobile into the game as I do intend to share this with my friends once im done, so some of my coding differs from the instructors since I have also gone through other sources to get lessons on how to integrate mobile controls. The issue is not from them however, it seems to be from the line attempting to access the rigidbody of the projectile laser from the player. This is my code so far…

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour

{

//remember to improve the laser upgrade system

//Variables

[SerializeField] float moveSpeed = 10f;

[SerializeField] float padding = 0.8f;

//adding upgrade system later

[SerializeField] GameObject laserPrefabOne;

[SerializeField] GameObject laserPrefabTwo;

[SerializeField] GameObject laserPrefabThree;

[SerializeField] float projectileSpeed = 10f;

float xMin;

float xMax;

float yMin;

float yMax;



void Start()

{

    SetUpMoveBoundaries();

    

}

// Update is called once per frame

void Update()

{

    Move();

    Fire();

}

//made public so it could be assigned to a button on UI for android

public void TakeShot()

{

    GameObject laser = Instantiate(laserPrefabOne, transform.position, Quaternion.identity);

    laser.GetComponent<RigidBody2D>().velocity = new Vector2(0, projectileSpeed);

}

private void Fire()

{

    if(Input.GetButtonDown("Fire1"))

    {

        TakeShot();

    }

}

private void Move() 

{

    

    //move the player and using cross platform to intigrate with UI joystick for android 

    var deltaX = CrossPlatformInputManager.GetAxisRaw("Horizontal") * Time.deltaTime * moveSpeed;

    var deltaY = CrossPlatformInputManager.GetAxisRaw("Vertical") * Time.deltaTime * moveSpeed;

    //Clamp to screen

    var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);

    var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);

    transform.position = new Vector2(newXPos, newYPos);

}

private void SetUpMoveBoundaries()

{

    Camera gameCamera = Camera.main;

    xMin = gameCamera.ViewportToWorldPoint(new Vector3(0,0,0)).x+padding; 

    xMax = gameCamera.ViewportToWorldPoint(new Vector3(1,0,0)).x-padding;

    yMin = gameCamera.ViewportToWorldPoint(new Vector3(0,0,0)).y+padding;

    yMax = gameCamera.ViewportToWorldPoint(new Vector3(0,1,0)).y-padding;

}

}

I don’t see any issue with your code, Can you describe which is the error, please? Sharing the console error can help too.

Quick Tip
When adding a reminder to your code, like the one you put in here:

//remember to improve the laser upgrade system

The common practice is to start the comment with TODO. If you are using Visual Studio as your selected IDE, it will make it stand out by changing the color and you can later search for that compound word making it really easy to know what you had in your backlog, or for others to search for that, really helpful when working in a team.

[11:29:53] Assets/Scripts/Player.cs(42,28): error CS0246: the type or namespace name ‘RigidBody2D’ could not be found (are you missing a using directive or an assembly reference?) is the error code it gave me. I am using a newer version than the instructor (2020.1.17f1) because I had downloaded a pack for this version ‘supposedly’ containing the right JDK, SDK, NDK and Gradle versions but that’s a different problem.

Oh! Now I get what the issue is, sorry for not noticing before.

You misspelled Rigidbody, it only has two capital letters, “R” and “D” and you added a third one, “B”. The line should look like this: laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);

Thank you, I swear this is the third time a spelling error has gotten me held up for a long while… is there any extension i could add to help explain or point out misspellings or was that error message basically spellcheck?

The error is saying that there’s nothing called RigidBody in the entire project, so yes, it’s basically a spell check.

Visual Studio immediately marks it as an error as long as you have the Unity Tools installed. Visual Studio Code, at least on my Mac, sometimes works and sometimes it doesn’t, that’s why I don’t like it, that might be your issue and I have no clue as to how to fix that, sorry.

If you are using VS (and it is working correctly) you can hover your mouse cursor over the error, it will show a small window.

Click on “Show Potential Fixes”, it will show some ways to correct your error which includes spelling errors.

Fixes

Thank you for the help

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms