Based on this Unity Animator API, there's a public function named IsInTransition. but why i cannot use it?
When i try to code in MonoDevelop, the Autocompletion wont work and also build was Error :
Assets/Scripts/CatAnimator.cs(32,18): error CS1061:
Type `Animator' does not contain a definition for `isInTransition'
and no extension method `isInTransition'
of type `Animator' could be found.
Are you missing an assembly reference?
any idea ?
The Complete Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Animator : MonoBehaviour {
Animator myAnimator;
public float speed = 10.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
float currSpeed, Param1;
bool Param2, Param3;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController> ();
myAnimator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
Param1 = 0;
Param2 = false;
Param3 = false;
if (controller.isGrounded) {
//==
}
if (myAnimator.IsInTransition (0)) { //ERROR HERE...
}
}//==update
}//==class
The problem here was the fact that you are making a class named Animator. However there is an animator class already provided by Unity. When you declare an object of type Animator (Animator myAnimator;), the compiler thinks of your class instead of the class provided by Unity. And in your class there is no IsInTransition() method to use. To fix this simply rename your class to MyAnimator for example.
Related
I have been working on unity's new ınput system and I have issue.I want use OnMove function of Player Input component but I'm getting this error:
MissingMethodException: PlayerMovement.OnMove Due to: Attempted to access a missing member.So my character don't move.How can I fix this ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
Vector2 moveInput;
Rigidbody2D myRigidbody;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed,
myRigidbody.velocity.y);
myRigidbody.velocity = playerVelocity;
}
}
What is a TileVania?
I would expect a InputAction.CallbackContext there I guess.
Sounds to me like the PlayerInput is trying to call your method via SendMessage but the signature is not correct and it should rather be
void OnMove(InputAction.CallbackContext context)
{
moveInput = context.Get<Vector2>();
}
You can fix this by adding the
[RequireComponent(typeof(Rigidbody2D))]
attribute to your PlayerMovement script. This will make sure that your script always has a Rigidbody2D component when it's added to a game object.
As the title says: I'm working on a project and no matter what I do when EDIT-> Thank you so much, I forgot that you can't have it outside the class
I use 'Rigidbody2D' and I get this error
CS0116: A namespace cannot directly contain members such as fields or methods
for the 3 variables before the class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public float movementSpeed = 1f;
public Vector2 movement;
private Rigidbody2D rb;
public class movement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
movement.x = Imput.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
void FixedUpdate(){
rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime);
}
}
"So, move the fields into the class. Fields (etc) cannot exist outside a class, as the compiler is telling you in the error" Thank you stuartd
When I try adding Rigidbody2D to the script it keeps giving me the error CS0428: "Cannot convert method group 'GetComponent' to non-delegate type 'Rigidbody2D'. Did you intend to invoke the method?". MAybe its something im not seeing but ive went through it several times and didnt see anything wrong.
using UnityEngine;
public class characterController : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
I think you need to add brackets after Rigidbody2D like this:
using UnityEngine;
public class characterController : MonoBehaviour {
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
To avoid this problem again, try reading the error message carefully. For example, this problem could have been easily avoided by reading docs on GetComponents.
You missed the parentheses after GetComponent
using UnityEngine;
public class characterController : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * 500);
}
}
}
Instead of running the GetComponent() method and setting rb as what it returns, the code tries setting it as a delegate.
the code is not working on the GameObject
public class EnemyMovement : MonoBehaviour
{
public int rotateSpeed = 1;
public int movementSpeed = 1;
private Transform myTransform;
public Transform target;
// Start is called before the first frame update
void Start()
{
GameObject go = GameObject.FindGameObjectsWithTag("Player");
target = go.transform;
myTransform;
}
// Update is called once per frame
void Update()
{
Rotate();
}
void Rotate ()
{
Debug.DrawLine(myTransform.position, target.position, Color.red);
}
}
the error goes here
GameObject go = GameObject.FindGameObjectsWithTag("Player");
target = go.transform;
GameObject.FindGameObjectsWithTag("Player") returns an array of objects with type GameObject: See the documentation. Since your GameObject go is not an array, but a single GameObject, you're getting an error saying the compiler can't implicitly convert GameObject[] to GameObject.
If you only need one object, you may be looking for GameObject.FindWithTag which is called like this:
GameObject go = GameObject.FindWithTag("Player");
How can you pass a Monobehaviour inside an instance of a non Monobehaviour class? I found this link where TonyLi mentions that you can pass a Monobehaviour to start and stop coroutines inside a instance of a class, but he does not show how you can do that. He does this theEvent.StartEvent(myMonoBehaviour); but he does not show where he gets myMonobehaviour from. I looked around on the internet but I cannot seem to find how.
Edit
Here is what I am trying to do. I want to run a coroutine inside an instance of a class. I also want to be able to stop the coroutine inside the instance of the class. I want to do it this way so that I don't have any objects in my scene that have large managers and also so that I can reuse the code for any object that I want to pingpong in this way. The code moves a Gameobject in one direction then takes a break and moves it in the other direction and takes a break again etc. But I cannot start the coroutine from outside the class.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent (typeof(Image))]
public class SpecialBar : MonoBehaviour {
public float rangeX;
public float breakTime;
public float step;
float startProgress = 0.5f;
PingPongGameObject pingPonger;
Color[] teamColors = new Color[]{new Color(255,136,0),new Color(0,170,255)};
void Start()
{
for(int i = 0; i < teamColors.Length; ++i)
{
teamColors[i] = StaticFunctions.NormalizeColor (teamColors[i]);
}
pingPonger = new PingPongGameObject (gameObject.transform.position,
new Vector3(rangeX,0.0f,0.0f),
gameObject,
startProgress,
breakTime,
step
);
}
}
The second class is where my coroutine is in.
public class PingPongGameObject
{
float step;
Vector3 center;
Vector3 range;
GameObject ball;
float progress;
float breakTime;
Vector3 endPos;
Vector3 oppositePosition;
public PingPongGameObject(Vector3 _center, Vector3 _range, GameObject _ball, float _startProgress, float _breakTime, float _step)
{
center = _center;
range = _range;
ball = _ball;
progress = _startProgress;
breakTime = _breakTime;
step = _step;
endPos = center - range;
oppositePosition = center + range;
// This is where I want to start the coroutine
}
public IEnumerator PingPong()
{
while (progress < 1) {
progress += Time.deltaTime * step;
Vector3 newPos = Vector3.Lerp (oppositePosition, endPos, progress);
ball.transform.position = newPos;
yield return null;
}
Vector3 temp = endPos;
endPos = oppositePosition;
oppositePosition = temp;
progress = 0;
yield return new WaitForSeconds (breakTime);
yield return null;
}
public float Step
{
set{step = value;}
}
public void StopCoroutine()
{
// This is where I want to stop the coroutine
}
}
TonyLi mentions that you can pass a Monobehaviour to start and stop
coroutines inside a instance of a class, but he does not show how you
can do that. He does this
You are can do that with the this keyword. The this keyword will get the current instance of MonoBehaviour.
In this example there's a tree, which happens to have a component MonoScript:
That particular instance of MonoScript can if it wants (since it's a c# program) instantiate a general c# class, NonMonoScript:
Class to pass MonoBehaviour from:
public class MonoScript : MonoBehaviour
{
void Start()
{
NonMonoScript nonMonoScript = new NonMonoScript();
//Pass MonoBehaviour to non MonoBehaviour class
nonMonoScript.monoParser(this);
}
}
Class that receives pass MonoBehaviour instance:
public class NonMonoScript
{
public void monoParser(MonoBehaviour mono)
{
//We can now use StartCoroutine from MonoBehaviour in a non MonoBehaviour script
mono.StartCoroutine(testFunction());
//And also use StopCoroutine function
mono.StopCoroutine(testFunction());
}
IEnumerator testFunction()
{
yield return new WaitForSeconds(3f);
Debug.Log("Test!");
}
}
You can also store the mono reference from the monoParser function in a local variable to be re-used.