SceneManager does not contain a definition - c#

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

Related

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)

ThirdPersonMovement script Unity

I was following this video: https://youtu.be/4HpC--2iowE?t=686
And after making the thirdpersonmovement script, I get an error that he did not get. Can anyone help me? There's probably a very small error that I cannot see, I must be blind. I followed the exact steps.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vetrical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
controller.Move(direction * speed * Time.deltaTime);
}
}
}
Error:
Assets\Scripts\ThirdPersonMovement.cs(16,57): error CS0103: The name 'Vertical' does not exist in the current context
Says error is on line 16. where "vector3 direction" is located
If the code is kinda wonky sorry, first time using this, was a weird set up for me.Thank you in advance.
There is a typo:
vetrical
vertical
t and r are swapped

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);
}
}
}

C# code not letting me use functions in their libraries

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));
}
}
}

Movement Script In Unity C#

I have been trying to make a movement script for my player in a 2D game but without success. I do not know why it is not working.
The problem is that the player isn't moving. I have a RigidBody attached and gravity on. (Not sure if gravity makes such a difference but I just thought to mention it.)
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float mx = Input.GetAxisRaw("Horizontal");
float mz = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(mx, 0.0f, mz);
Debug.Log(movement);
rb.AddForce(movement * speed * Time.deltaTime);
}
}
You may wanna make sure you are adding enough force to actually cause the player to move. Try increasing the force variable incrementally until you see a change. Hope this helps!

Categories