Unity referencing a Script gives weird error - c#

I am trying to reference a Script from another GameObject in Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jumpcheck : MonoBehaviour
{
GameObject Player;
fuckingMove fm;
// Start is called before the first frame update
void Start()
{
Player = GameObject.Find("Player");
fm = Player.GetComponent<"fuckingMove">();
}
// Update is called once per frame
void OnTriggerEnter2D()
{
Player.fm.isGrounded = true;
}
void OnTriggerExit2D() {
Player.fm.isGrounded = false;
}
}
The problem is that I keep getting this error and i don't know why.
Assets/Scripts/jumpcheck.cs(16,49): error CS1525: Invalid expression term ')'
Can anyone explain please?

Generics expect type as parameter not type name as string.
//Incorrect
Player.GetComponent<"ComponentName">();
//Correct
Player.GetComponent<ComponentName>();

Related

Unity says "error CS1022: Type or namespace definition, or end-of-file expected"

I am making a cross board game using Unity based on a tutorial. This problem occurs when I add a jump animation.
All my code is from that tutorial and no changes have been made except for one value.
I know which line of code is wrong but I don't know why and I don't know how to fix it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour
{
private Rigidbody2D rb;
public float jumpAmount = 1;
bool isGrounded;
public Transform groundCheck;
public LayerMask groundlayer;
public Animator anim;
public float overlap = 0.5f;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded)
{
Jump();
}
}
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, overlap, groundlayer);
}
void Jump()
{
rb.AddForce(Vector2.up *5* jumpAmount, ForceMode2D.Impulse);
}
if(isGrounded)
{
anim.SetBool("Jump",false);
}
else
{
anim.SetBool("Jump", true);
}
}
Does anyone know what's wrong with this, please?
Thank you in advance to those who answered!
Assets\Script\Playermovement.cs(52,1): error CS1022: Type or namespace definition, or end-of-file expected
This is the error message I received. I know it tells me the error occurred on the first character of line 52 (which is the last line), but other than that I don't know anything because there is only one curly bracket on that line.
The problem is with if (isGrounded) statement. The if/else statement needs to be in a method/function, and can't work without being in one.

Can' instantiate a prefab in a certain time

I'm trying to instantiate a prefab in a certain time, but that gives me an error: "object reference not set to an instance of an object".
This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class animatronicsManager : MonoBehaviour
{
clockTime clockTime;
public GameObject enemy;
void Start()
{
clockTime.FindObjectOfType<clockTime>();
}
void Update()
{
if (clockTime.time2 == 10)
{
Instantiate(enemy, new Vector3(3.66f, -1, 0.66f), Quaternion.Euler(0, -190, 0));
}
}
}
i also tryed to to spawn it by pressing a key, and it works. Can you help me resolve this?
("time2" is an int variable that count time)
Thank you
clockTime variable is never initialized in your code. This is the reason of the error. Initialize it in in your Awake() (or anywhere before trying to access in the Update() method)
I suppose you wanted your Start to be like this
void Start()
{
clockTime = FindObjectOfType<clockTime>();
}

Type for Cinemachine in Unity

So I have a freelook camera, and I want it so that it only turns when the player drags the right mouse button. The problem is, since Cinemachine is a plugin, I'm not sure what type I should set the FreeLook Camera component. I want to make something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine.Utility;
public class lookOnDrag : MonoBehaviour
{
// Not sure what type to set CFL
private Cinemachine CFL;
void Start()
{
CFL = GetComponent<CinemachineFreeLook>();
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
CFL.enabled = true;
}
else
{
CFL.enabled = false;
}
}
}
I've checked the documentation thoroughly and don't have an answer.
You have probably entered the above reference incorrectly and it does not recognize it correctly. using only Cinemachine and delete utility.
using Cinemachine;
Also define the variable according to the component or CinemachineVirtualCameraBase;
private CinemachineFreeLook CFL;
void Start()
{
CFL = GetComponent<CinemachineFreeLook>();
}

How to reference an Object in Unity with C#?

Hy Guys
I'm getting the error CS0120 in Unity at following Code:
Error CS0120: An object reference is required for the non-static
field, method, or property 'PortalScript.Spawn()'
Script 1: here I try to create a new GameObject on Screen with a certain distance to Player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PortalScript : MonoBehaviour
{
public GameObject Portal; // referenced at Inspector
GameObject Player = GameObject.Find("Player");
public void Spawn()
{
bool portalSpawned = false;
while (!portalSpawned)
{
Vector3 portalPosition = new Vector3(Random.Range(-7f, 7f), Random.Range(-4f, 4f), 0f);
if((portalPosition - Player.transform.position).magnitude < 3)
{
continue;
}
else
{
// Instantiate at position.
Instantiate(Portal, portalPosition, Quaternion.identity);
portalSpawned = true;
}
}
}
}
Script 2: This script is on the Player. On Case it should call the method Spawn from script 1
public class Point : MonoBehaviour
{
public PortalScript Spawn;
void Update()
{
score = updateScore;
switch (score)
{
case 1:
PortalScript.Spawn(); // ERROR at this line
break;
}
}
If I write the code from script 1 directly into script 2, it works.
My brain stops at that point. Thanks for all your help and let me know if you need more information.
Replace this line:
PortalScript.Spawn(); // ERROR at this line
with this:
Spawn.Spawn();

How could i fix an error (cs0120) in unity?

In unity, I'm trying to make a press of a button increase the speed of the player. However, each time I run it. It gives me:
Error CS0120: An object reference is required for the non-static
field, method, or property 'PlayerController.speed'
I've already tried changing order of the code, so what could I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Upgrader1 : MonoBehaviour
{
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
public class Upgrader1 : MonoBehaviour
{
PlayerController PlayerController; //It should be member variable
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
it's always a good thing to use proper naming conventions.
PlayerController _PlayerController;
void Start() {
GameObject Player = GameObject.Find("Player");
_PlayerController = Player.GetComponent<PlayerController>();
}
public void UpgradeSpeed() // I changed the name according to its functionality
{
_PlayerController.speed++;
}
With this, you won't put PlayerController class reference again by mistake.

Categories