C# code not letting me use functions in their libraries - c#

This is my simple code - I'm using Visual Studio 2017, and assume the issue is with the libraries that contain the functions Input."" are not being found.
There is no auto complete, when I start to type Input."" and there are no colours indicating it's a prewritten function. Could anyone help?
I've been stuck trying to figure out how to make this work and I'm completely stalemated (btw even when I type 0.5f it does not colour up like it does when I watch other people code this).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horiztonal") > 0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horiztonal") * moveSpeed * Time.deltaTime,0f,0f));
}
}
}

Related

SceneManager does not contain a definition

Got a problem with using the scene manager. Already had a using UnityEngine.SceneManagement still shows the error of both LoadScene and GetActiveScene. I don't have a class such as "SceneManager". Here is the code.
using UnityEngine.SceneManagement;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
bool alive = true;
public float speed = 5f;
public Rigidbody rb;
float horizontalInput;
float horizontalMultiplier = 1.7f;
private void FixedUpdate()
{
if (!alive) return;
Vector3 forwardMove = transform.forward * speed * Time.fixedDeltaTime;
Vector3 horizontalMove = transform.right * horizontalInput * speed * Time.fixedDeltaTime * horizontalMultiplier;
rb.MovePosition(rb.position + forwardMove + horizontalMove);
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if (transform.position.y < -5)
{
end();
}
}
public void end ()
{
alive = false;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Here is the error I have.
First of all try:
UnityEngine.SceneManagement.SceneManager.LoadScene( "myScene");
There can be 2 troubles:
First -
This happened to me. I had downloaded an asset off the store called Falling Leaves. It included a script called SceneManager. Namespaces should be better utilized for store assets. I'll have to take a look at the code to my own to make sure I'm not breaking someone else's project unintentionally.
Second - if you are wirtting kinda your own lib for unity check that all .dll / projects was imported or if you are writting code in folder that marked with .asmdef this is tool in unity that helps with faster compile and more separeted development (there) and if you are do smth with this asmdef file and checkbox "include unity engine" is not setted as true this is too can be reason so check pls
Same question was answered there https://forum.unity.com/threads/scenemanager-does-not-contain-a-definition-for-loadscene.388892/
I believe you have to be using UnityEngine; before actually using UnityEngine.SceneManagement
here's an example
using UnityEngine; using UnityEngine.SceneManagement;
the problem here is that your using a part or branch or something of EnityEngine before using UnityEngine itself

Character in Unity scene walking backwards?

I am currently working on the Flooded Grounds Unity Asset, I'm implementing an Alien NPC, with a script that tells it to follow the player.
The problem that I'm encountering is that the Alien turns his back to the player and I can't figure out why. I also tried rotating it by 180° on the Y axis (both in the transform and inside of the script using transform.rotate.y - when using the transform doesn't make any difference while using it in the update function just makes it constantly snap back and forth).
Here's a video of the issue:
https://drive.google.com/file/d/1tC9zJWKXXDuNZNZJbl2htBUgTPSTB4IG/view?usp=sharing
And here's the script that I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
public NavMeshAgent enemy;
public Transform Player;
private Rigidbody alienRb;
private Animator alienAnim;
// Start is called before the first frame update
void Start()
{
alienRb = GetComponent<Rigidbody>();
alienAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
enemy.SetDestination(Player.position);
if(enemy.velocity.magnitude > 0)
{
alienAnim.SetTrigger("Walk");
}
}
}
Any help would be very appreciated!
Hello Mazza ^^ First of all, I have to say that your alien monster is really cute :D Anyway, did you try transform.LookAt(Player.transform.position)? Also, transform.rotation is might not be affective, maybe you can try alien.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self); or transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 180f, transform.rotation.z)

When I try to transform this player controller it just resets to the original position

This is a reduced version of the player controller I am using now, which still produces the error explained below:
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private float m_RunSpeed;
private CharacterController m_CharacterController;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
Vector3 move = Vector3.right * m_RunSpeed;
m_CharacterController.Move(move * Time.fixedDeltaTime);
}
}
}
I am using the standard assets script and I am trying to have a script teleport the player to different places. When I try and move the player it goes to that position for a frame, then goes right back to its position.
player.transform = new Vector3(1,2,3);
// works as expected, but then next frame, player's
// position is back to where it was before
This problem occurs because Move may not be able to read the up-to-date values for transform.position.
This problem has been reported on the official Unity Issue Tracker here, and a solution was posted there as well:
The problem here is that auto sync transforms is disabled in the physics settings, so characterController.Move() won't necessarily be aware of the new pose as set by the transform unless a FixedUpdate or Physics.Simulate() called happened in-between transform.position and CC.Move().
To fix that, either enable auto sync transforms in the physics settings, or sync manually via Physics.SyncTransforms right before calling Move()
So, you could fix your problem by editing FixedUpdate so that it calls Physics.SyncTransforms before Move, like this:
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private float m_RunSpeed;
private CharacterController m_CharacterController;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
Vector3 move = Vector3.right * m_RunSpeed;
Physics.SyncTransforms();
m_CharacterController.Move(move * Time.fixedDeltaTime);
}
}
}

"Predefined type System.Void is not defined or imported" error trying to compile c# scripts for Unity 2D in Visual Studio 2017

I'm trying to learn creating 2D videogames with Unity but i can't compile my script for CharacterMovement because of several errors.
Even creating a new empty script, the compiler says that "Predefined type System.Void is not defined or imported" and i couldn't find online a way to fix this.
This is the empty script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
And this is the script i'm trying to compile :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void Update () {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if(horizontalMove == 0)
{
animator.SetBool("Jumping", false);
}
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("Jumping", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
animator.SetBool("Crouching", true);
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
animator.SetBool("Crouching", false);
}
}
void FixedUpdate ()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
(On my script i get about 60 errors all similar)
change the option in File->BuildSettings->playersettings-api compatibility level to .Net 4X
This happened when I tried to install Microsoft.Toolkit.Uwp.Notification using Install-Package Microsoft.Toolkit.Uwp.Notifications -Version 7.1.2.
Once I do this, I notice a new config file is added to my solution explorer. I have to delete this to fix the error.
Another possible solution for those stumbling across this in the future: Go to Edit->Preferences->External Tools->Regenerate project files to bring back the original CS Project & VS solution files.
That was the only option that worked after trying other common answers like changing API compatibility, upgrading VS, or deleting the project obj folder. I think it broke like that due to me trying to clean out the solution manually and Unity ended up corrupting some packages after I attempted to delete them. So unfortunately it seems you will get this error if you try to do that.

Unity2D & CSharp: MSBuild process could not be started

I'm trying to build my first game in Unity 2D.
OS: Windows 10
I reached the moment where I'm supposed to program the controls for a character.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W))
{
GetComponend<RigidBody2D> ().velocity = new Vector2 (0, jumpHeight);
}
}
}
Trying to create a build ends with this error text:
> Build failed. MSBuild process could not be started
Please have a look at the screen shot below:
Screenshot
How can I solve this problem?

Categories