I'm new to Unity3D , Getting compiler errors - unity3d

I am new to Unity and I'm making a stickman game I added a balance script.
But it shows a compiler error "The variable of rb has not been assigned".
Can u please tell me how to assign a variable?
Here's the code-
// Update is called once per frame
void Update()
{
rb.MoveRotation(Mathf.LerpAngle(rb.rotation, targetRotation, force * Time.fixedDeltaTime));
}

Because you didn't to use a Rigidbody you need to get the component from the GameObject and in your case make the actual Rigidbody variable you want to use.
Your code should look like that:
using UnityEngine;
public class Example : MonoBehaviour
{
Rigidbody rb;
void Start()
{
//Fetch the Rigidbody from the GameObject with this script attached
rb= GetComponent<Rigidbody>();
}
void Update()
{
rb.MoveRotation(.....);
}
}

Related

Why more balls are instantiating?

I'm making a game in unity where the user drags to shoot a ball at some objects at a distance. So far I have this DragAndShoot script:
//using System.Collections;
//using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Collider))]
public class DragAndShoot : MonoBehaviour
{
public Transform prefab;
private Vector3 mousePressDownPos;
private Vector3 mouseReleasePos;
private Rigidbody rb;
private bool isShoot;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnMouseDown()
{
mousePressDownPos = Input.mousePosition;
}
private void OnMouseUp()
{
mouseReleasePos = Input.mousePosition;
Shoot(mouseReleasePos-mousePressDownPos);
}
private float forceMultiplier = 3;
void Shoot(Vector3 Force)
{
if(isShoot)
return;
rb.AddForce(new Vector3(Force.y,Force.x,Force.z) * forceMultiplier);
isShoot = true;
createBall();
}
void createBall(){
Instantiate(prefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
}
}
As you can see, I made the function createBall() in order to respawn a ball prefab at the position of the game object SpawnPoint. When I run the game, the first ball shoots fine. And another ball respawns.
Issue: when I shoot the second ball and it moves, one more ball seems to have appeared at the second ball somehow, and it moves as well. Not sure why this is happening and how to fix it - can someone pls help? Thanks.
The problem is that you need to Destroy() the game object you threw first. Since you are just bringing the objects back when click down again, here is what you should do:
Make it so that it destroys the old object. Because you just keep instantiating the object, then when you throw it again it throws the old one too. If you understand what I mean, then hopefully, you can turn this into what you want. (It wasn’t exactly clear what your game was; this is what I interpreted)

How to call a method every time, when player stay at any collider?

I have a game like CubeSurfer. When player goes through an collectable, two methods are called (for "jumping" and creating a cube under player). But, when player goes through second, or third collectable, nothing happens. I tried to attached script to collectable, and tried to put the method "OnTriggerEnter" in the body of methods "Start" and "Update". But it doesn't work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCollider : MonoBehaviour
{
[SerializeField] Collider player;
[SerializeField] GameObject destroyIt;
[SerializeField] GameObject objToClone;
[SerializeField] Transform rootObjInScene;
Transform curParent;
Vector3 posOffset, posPlayer;
float distance = 1f;
private void OnTriggerStay(Collider player)
{
PlayerJumping();
CreateAnother();
Destroy(destroyIt);
}
public void PlayerJumping()
{
posPlayer = Vector3.up * distance;
Vector3 playerPos = rootObjInScene.position + posPlayer;
rootObjInScene.transform.position = playerPos;
}
public void Awake()
{
curParent = rootObjInScene;
posOffset = Vector3.down * distance;
}
public void CreateAnother()
{
Vector3 newPos = curParent.position + posOffset;
GameObject newObj = Instantiate(objToClone, newPos, Quaternion.identity, curParent);
curParent = newObj.transform;
}
}
You should also check your Rigibody collision detection mode in the inspector.
By default, this is set to Discrete, but if your object is fast-moving, you should set it to Continuous or ContinuousDynamic, otherwise the collisions won't be registered because the object is moving too quickly.
These are a little bit performance intensive, so I'd still recommreading up on the Unity documentation or digging through a couple of good articles to know what's best for you :)
You shouldn't call OnTriggerEnter explicitly (in your Update()), it's an event message and gets called automatically by unity when you enter a Collider marked isTrigger.
You can use OnTriggerEnter only if your collider is marked isTrigger, if you want to get a collision with a Collider that is not explicitly a trigger, you use OnCollisionEnter
To execute a method repeatedly while touching a Collider use OnCollisionStay, while touching or inside a Collider with isTrigger use OnTriggerStay
OnCollisionStay
OnTriggerStay
You can use OnCollisionStay or OnTriggerStay
void OnCollisionStay(Collision collisionInfo)
{
//Do something when collisionInfo.gameObject stay in this.gameObject collider
}

Simple unity moving function doesn't work

So I have assigned a script to a simple ball and tried to attach this code:
private Rigidbody2D rb2D;
Vector2 speed;
void Start()
{
rb2D = gameObject.AddComponent<Rigidbody2D>();
speed = new Vector2(1,0);
}
void FixedUpdate()
{
rb2D.AddForce(speed);
}
And It doesn't work. May I know what I did wrong ? As it seems that addforce doesn't work. And the object with script attached has a rigidbody2d component

Unity Script error CS1061 'Vector2' does not contain a definition for 'GetAxis'

The full error message I am receiving in Unity and I am working on a mac.
Assets/Scripts/Controller.cs(19,45): error CS1061: 'Vector2' does not contain a definition for 'GetAxis' and no accessible extension method 'GetAxis' accepting a first argument of type 'Vector2' could be found (are you missing a using directive or an assembly reference?)
Here is the code I am using:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float speed;
private Vector2 moveVelocity;
private Rigidbody2D rigidBody;
void Start() {
rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() {
Vector2 moveInput = new Vector2(moveInput.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
moveVelocity = moveInput.normalized * speed;
}
void fixedUpdate () {
rigidBody.MovePosition(rigidBody.position + moveVelocity * Time.fixedDeltaTime);
}
}
Please let me know if there is any additional information needed.
I think you got Input and moveInput confused. Also, if you have a RigidBody or RigidBody2d you should try and not modify the position of GameObjects directly, instead using the AddForce method. I would remove the fixed update method and instead modify your Update method to be the following:
void Update() {
Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"));
rigidBody.AddForce(moveInput.normalized * speed);
}

Unity Error CS0120

I just got into unity3d, and i was using a guide. But every time I try to compile, I get Error CS0120. Please just help me instead of marking it as duplicate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
Rigidbody.AddForce(movement*speed*Time.deltaTime);
}
}
This line is the issue
Rigidbody.AddForce(movement*speed*Time.deltaTime);
The error you are getting is that
error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3)'
If you wanted to AddForce to a rigidbody attatched to the same gameobject as your script, then you need to grab that ridigbody instance and apply force, like this.
this.GetComponent<Rigidbody>().AddForce(movement*speed*Time.deltaTime);