Assets\PipeMiddleScript.cs(25,18): error CS7036: There is no argument given that corresponds to the required formal parameter 'scoreToAdd' of 'LogicScript.addScore(int)'
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Related
Tell me please. I have 2 scripts that hang on different Unity objects. The first script is for clicking a button. The second one is for executing the function.
How can I execute the AddItem function if the button is pressed?
Script 1 (button click):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
Script 2 (Adding Items):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventoryManager : MonoBehaviour
{
public void AddItem(ItemScriptableObject _item, int _amount)
{
foreach(InventorySlot slot in slots)
{
if (slot.item == _item)
{
slot.amount += _amount;
return;
}
}
foreach(InventorySlot slot in slots)
{
//print(_item+" "+_amount);
if (slot.isEmpty == true)
{
slot.item = _item;
slot.amount = _amount;
slot.isEmpty = false;
slot.SetIcon(_item.icon);
return;
}
}
}
If you dont have a variable on the second script, of the first script, you should do that by using:
GameObject go;
go.GetComponent<InventoryManager>();
try changing the first script to something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
void Update()
{
if (isClick)
{
Debug.Log("do something")
}
}
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
This way you can modify the variable: is Click easily.
Im writing lobby script, there is a error in title. My code:
Copying scripts from https://youtu.be/EPJIJ_vTXu4?t=750
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
public InputField IFNameRoom;
public void CreateRoom() {
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 4;
PhotonNetwork.CreateRoom(IFNameRoom.text, roomOptions);
}
public void JoinRoon() {
PhotonNetwork.JoinRoon(IFNameRoom.text);
}
public override void OnJoinedRoom() {
PhotonNetwork.LoadLevel("Game");
}
}
For starters there is a typo:
JoinRoom not JoinRoon
I know this question has been asked before, here and here.
But I still can't get variables from another script. I don't know what I'm doing wrong.
*(I'm really new to programming in general so I might have missed something glaringly obvious)
I keep on getting the error: The name 'points' does not exist in the current context
The slimespawner script is on the Canvas.
Sorry if the question is too simple.
here's the script I'm trying to access:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class slimespawner : MonoBehaviour
{
public int points;
public Text score;
public float xx;
public float yy;
void Start()
{
points = 0;
xx = Random.Range(-32f, 32f);
yy = Random.Range(-18.5f, 18.5f);
}
void Update()
{
score.text = "Score: " + points.ToString();
}
}
And here's the script that's trying to use the points variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slimecontroller : MonoBehaviour
{
private float movespeed = 0.1f;
public slimespawner slisp;
void Start()
{
slisp = GameObject.Find("Canvas").GetComponent<slimespawner>();
}
void Update()
{
points += 1;
}
}
Your code should look as this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slimecontroller : MonoBehaviour
{
private float movespeed = 0.1f;
public slimespawner slisp;
void Start()
{
slisp = GameObject.Find("Canvas").GetComponent<slimespawner>();
}
void Update()
{
slisp.points += 1;
}
}
Access the property with slisp.points += 1;
Hello I'm currently making a game at the moment that's like pacman but multiplayer. I'm having trouble on how to network the scores. I'm wanting to show own score on Local host and own score on Client. So far I could only update scores for the Local Host. Could anyone please help?
Score Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class ScoreManager : NetworkBehaviour {
public static int score;
Text text;
void Start()
{
text = GetComponent<Text>();
score = 0;
}
void Update()
{
if (!isServer)
{
return;
}
text.text = "Score: " + score;
}
}
Ontriggerstart for score
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PacDot : MonoBehaviour {
void OnTriggerEnter2D(Collider2D co)
{
if (co.tag == "UFO")
{
Destroy(gameObject);
ScoreManager.score++;
}
}
}
Player Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class UFOMove : NetworkBehaviour {
public float speed;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (!isLocalPlayer)
{
return;
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rb2d.AddForce(movement * speed);
}
}
I have had no previous issues creating custom editors, however with this one it seems to be informing me "Multi-object editing not supported" the code for my script is below as well as the script for my custom editor. Is there something that I'm missing?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Biophase Games/UI/Togglr")]
[System.Serializable]
public class Togglr : MonoBehaviour {
public List<Toggler> toggles;
public ColorBlock onColors;
public ColorBlock offColors;
public string value;
void Update() {
foreach(Toggler t in toggles) {
if (t.toggle.isOn == true) {
value = t.text;
t.toggle.colors = onColors;
} else {
t.toggle.colors = offColors;
}
}
}
}
[System.Serializable]
public class Toggler {
public Toggle toggle;
public string text;
}
and the CustomEditor script
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(Togglr))]
[CanEditMultipleObjects]
public class TogglrEditor : Editor {
SerializedProperty onColors;
SerializedProperty offColors;
SerializedProperty toggles;
void OnEnable() {
onColors = serializedObject.FindProperty ("onColors");
offColors = serializedObject.FindProperty ("offColors");
toggles = serializedObject.FindProperty ("toggles");
}
public override void OnInspectorGUI() {
serializedObject.Update ();
EditorGUILayout.PropertyField (toggles, true);
EditorGUILayout.PropertyField (onColors);
EditorGUILayout.PropertyField (offColors);
serializedObject.ApplyModifiedProperties ();
}
}
On an attempt to solve my issue, I moved the class Toggler into it's own file which subsequently solved the issue I was having.