I'm having an error on line 19 Character 16, I am getting a CS0246 error, this code is taken from a public character controller so I'm not sure what's happening. The error also shows up on line 14 character 52 for IPlayerController.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using TarodevController;
namespace TarodevController
{
/// <summary>
/// Hey!
/// Tarodev here. I built this controller as there was a severe lack of quality & free 2D controllers out there.
/// Right now it only contains movement and jumping, but it should be pretty easy to expand... I may even do it myself
/// </summary>
public class PlayerController : MonoBehaviour, IPlayerController
{
// Public for external hooks
public Vector3 Velocity { get; private set; }
public FrameInput Input { get; private set; }
public bool JumpingThisFrame { get; private set; }
public bool LandingThisFrame { get; private set; } = false;
public Vector3 RawMovement { get; private set; }
public bool Grounded => Cooldown;
private Vector3 _lastPosition;
private float _currentHorizontalSpeed, _currentVerticalSpeed;
}
}
Thank You if you know what's happening, the code is referenced again in the input section, for timing the jump.
private void GatherInput()
{
Input = new FrameInput
{
JumpDown = UnityEngine.Input.GetButtonDown("Jump"),
JumpUp = UnityEngine.Input.GetButtonUp("Jump"),
X = UnityEngine.Input.GetAxisRaw("Horizontal")
};
if (Input.JumpDown)
{
_lastJumpPressed = Time.time;
}
}
Here's the full code if it helps, https://github.com/Matthew-J-Spencer/Ultimate-2D-Controller/blob/main/Scripts/PlayerController.cs
Edit, Here is the error message:
PlayerController.cs(19,16): error CS0246: The type or namespace name 'FrameInput' could not be found (are you missing a using directive or an assembly reference?)
The necessary functions exist in the two other scripts that Tarodev has in the scripts folder of the git repo, I imported both and no errors come up for me.
Related
This is my full script
I get an error on line 15, beginning at the "PlayerControls" word.
error CS0246: The type or namespace name 'PlayerControls' could not be found (are you missing a using directive or an assembly reference?)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace puggy
{
public class InputHandler : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmount;
public float mouseX;
public float mouseY;
PlayerControls inputActions;
Vector2 movementInput;
Vector2 cameraInput;
public void OnEnable()
{
if (inputActions == null)
{
inputActions = new PlayerControls();
inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
}
inputActions.Enable();
}
private void OnDisable()
{
inputActions.Disable();
}
public void TickInput(float delta)
{
MoveInput(delta);
}
private void MoveInput(float delta)
{
horizontal = movementInput.x;
vertical = movementInput.y;
moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
mouseX = cameraInput.x;
mouseY = cameraInput.y;
}
}
}
I'm guessing it wants a reference for "PlayerControls". I'm confused though because I followed a walkthrough, I ran into other issues but was able to resolve them pretty quickly. This one is tripping me up though.
I'm expecting to be able to attach this script to my player model and have it move based on player input.
I have re-typed my script and followed other forums to see if anyone else has had similar issues.
EDIT:
public class PlayerControls has to be in a file called PlayerControls.cs!
Looking at your screenshot you have InputHandler in a file called PlayerControls.cs
I guess you copied/renamed the class or file. Please make sure Filename and Classname match!
I figured it out. When making the "PlayerControls Input Action" I forgot to also click "generate C# class".
This makes sure that all the stuff I set up in the GUI, is applied into the C# script.
this is my error
Assets\Standard Assets\Characters\ThirdPersonCharacter\Scripts\AICharacterControl.cs(7,31):
error CS0118:'NavMeshAgent' is a namespace but is used like a type
This is my code I can't fix that :(
using System;
using UnityEngine;
namespace UnityStandardAssets.Characters.ThirdPerson.NavMeshAgent
{
[RequireComponent(typeof (NavMeshAgent))]
[RequireComponent(typeof (ThirdPersonCharacter))]
public class AICharacterControl : MonoBehaviour
{
public NavMeshAgent agent { get; private set; }
public ThirdPersonCharacter character { get; private set; }
public Transform target;
private void Start()
{
agent = GetComponentInChildren<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updateRotation = false;
agent.updatePosition = true;
}
private void Update()
{
if (target != null)
{
agent.SetDestination(target.position);
character.Move(agent.desiredVelocity, false, false);
}
else
{
character.Move(Vector3.zero, false, false);
}
}
public void SetTarget(Transform target)
{
this.target = target;
}
}
}
thank you
Don't use the names of existing types as namespaces. You have a custom namespace here:
namespace UnityStandardAssets.Characters.ThirdPerson.NavMeshAgent
So anything within that namespace (or in code which references the enclosing namespace) which refers to NavMeshAgent will be referring to the namespace. Not to the type in Unity.
Rename your namespace. For example:
namespace UnityStandardAssets.Characters.ThirdPerson.MyNavMeshAgent
(Or something more contextually meaningful.)
Basically the lesson here is that names matter. The names of your namespaces, types, variables, etc. should be clear and unambiguous to both you and to the compiler.
And, of course, to reference that type directly you'll need a using directive:
using UnityEngine.AI;
(In unity 2D)So I have a script for the amount of money and apples I have in my game and want to make more than 1 game object have the amount of apples script. btw there IS a void TransferMoney and IS public. I do this, and I need to transfer the amount of money i have to the apple script but because it's an array it does the following error: (55,16): error CS1061: 'apple[]' does not contain a definition for 'TransferMoney' and no accessible extension method 'TransferMoney' accepting a first argument of type 'apple[]' could be found (are you missing a using directive or an assembly reference?) Here's the money script:
public class numberofmoney : MonoBehaviour //script in the Text UI "amount of money" {
static public int scenemoney;
public string house;
public string shopString;
public TMP_Text moneyText;
public cookie1 Cookie;
public apple[] apples;
public void BoughtApple(int currentAOM)//aom stands for 'amount of money'
{
scenemoney = currentAOM;
}
void Awake()
{
apples = GameObject.FindObjectsOfType<apple>(); //finds apple
}
void Start()
{
Scene cookie = SceneManager.GetActiveScene();
house = cookie.name; //checks scene and does part of converting to string
Scene shop = SceneManager.GetActiveScene();
shopString = shop.name; //checks scene and does part of converting to string
}
public void forCookie(int money)
{
scenemoney = money;
}
void Update()
{
string scenemoneystring = scenemoney.ToString();
moneyText.SetText(scenemoneystring); //Converts money and sets text
if (house == "House") { //transfers money between scripts #1
Cookie.transferMoney(scenemoney);
}
if (shopString == "store") { //transfers money between scripts #2
apples.TransferMoney(scenemoney);
}
}
}
and apples script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;
public class apple : MonoBehaviour {
public string appleString;
public int publicMoney;
public int Apples;
public NumberOfApples numberOfApples;
public TMP_Text tmp;
public numberofmoney NumberOfMoney;
void Awake()
{
numberOfApples = GameObject.FindObjectOfType<NumberOfApples>();
NumberOfMoney = GameObject.FindObjectOfType<numberofmoney>();
}
public void TransferMoney(int money)
{
publicMoney = money;
}
void OnTriggerEnter2D(Collider2D trigger)
{
if (publicMoney >= 10){
Destroy(this.gameObject);
Apples++;
publicMoney -= 10;
appleString = Apples.ToString();
tmp.SetText(appleString);
NumberOfMoney.BoughtApple(publicMoney);
}
numberOfApples.transferApples(Apples);
}
}
For what I can see, you're doing:
apples.TransferMoney(scenemoney);
but apples is an array of the class apple, you can't call the "transferMoney" method, you need to iterate each object of the array and call the transferMoney method individually.
foreach(apple a in apples){
a.TransferMoney(scenemoney);
}
hope that helps!
Edited: copy paste error called by derHugo
I'm pretty new to coding and this script is giving me an error. It's suppose to pick up a key for a door.
It says it's error CS1061 'object' does not contain a definition for 'keyCount' and no accessible extension method 'keyCount' accepting a 1st argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
I tried double checking the script and matching it up to a tutorial I was watching. still no affect. I don't know how to make a definition (Idk where to look to find that).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class keyitems
{
public static int keyCount;
}
public class keyitem : MonoBehaviour
{
public object GameVariables { get; private set; }
void OnTriggerEnter(Collider collider)
{
{
if (collider.gameObject.name == "Player")
{
GameVariables.keyCount += 2;
Destroy(gameObject);
}
}
}
}
I would like to pick up a key to open a door. any help on this would be amazing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class keyitems
{
public static int keyCount;
}
public class keyitem : MonoBehaviour
{
public object GameVariables { get; private set; }
void OnTriggerEnter(Collider collider)
{
{
if (collider.gameObject.name == "Player")
{
keyitems.keyCount += 2;
Destroy(gameObject);
}
}
}
}
keyCount variable is keyitems's variable. So, you can get it like this :
keyitems.keyCount
And, I recommend you to use camel case when typing class name.
For example : KeyItems ( not keyitems )
So I'm trying to make some buttons for a "shop window" in my game. I cannot see where I went wrong in the code, but I get this error:
Assets/shopHandler.cs(34,17): error CS1061: Type
`UnityEngine.GameObject' does not contain a definition for
`transfrom' and no extension method `transfrom' of type
`UnityEngine.GameObject' could be found. Are you missing an assembly
reference?
My code:
using System.Collections;
using UnityEngine;
public class shopHandler : MonoBehaviour {
[System.Serializable]
public class Item
{
public string name;
public Sprite icon;
public float price;
public float dps;
public int acquired;
}
public Item[] shopItems;
public GameObject button;
// Use this for initialization
void Start () {
foreach (Item i in shopItems)
{
GameObject btn = (GameObject)Instantiate(button);
ItemScript scp = btn.GetComponent<ItemScript>();
scp.name.text = i.name;
scp.price.text = "Price: $" + i.price.ToString("F1");
scp.acquired.text = i.acquired.ToString();
scp.dps.text = "$/s: " + i.dps.ToString("F1");
scp.icon.sprite = i.icon;
btn.transfrom.SetParent(this.transform);
}
}
// Update is called once per frame
void Update () {
}
}
Because its "transform", not "transfrom". A little typo in your last line inside your start method.