Namespace '<global namespace>' already contains a definition for 'PlayerCollision' - c#

Error:
Assets\Scripts\PlayerCollision.cs(4,14): error CS0101: The namespace '' already contains a definition for 'PlayerCollision'
using UnityEngine;
public class PlayerCollision : MonoBehaviour {
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("We hit something");
}
}
}

You cannot name the MonoBehaviour script the same as an internal name(PlayerCollision).
Try a prefix like My:
public class MyPlayerCollision : MonoBehaviour {

You can put your PlayerCollision in a namespace of its own (and to avoid other namespace issues, do the same for other classes in your project):
using UnityEngine;
namespace MyGame
{
public class PlayerCollision : MonoBehaviour {
//your code here
}
}

Related

Problem with a Flappy Bird Clone in Unity

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

MainMenu.OnJoinedRoon(): no suitable method found to override Photon Pun

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

What is an `invalid token in class` error?

I've been using a tutorial guide for the game I'm making to figure out the basics and I ran into 2 errors while coding the collisions:
Invalid token "{" in class, struct, or interface member of declaration and
Type or namespace definition, or end of file expected.
Here is the code:
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
{
void OnCollisionEnter (Collision collisonInfo)
{
if (collisonInfo.collider.tag == "Obstacles")
movement.enabled = false;
}
}
}
Looks like you have too many curly braces:
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
void OnCollisionEnter(Collision collisonInfo)
{
if (collisonInfo.collider.tag == "Obstacles")
movement.enabled = false;
}
}

Unity3d:Add a public script via inspector

So I am creating a game, which is a Novel game. I want to add a functionality where my friend can add a new dialogue on a character game object. To do that, I already created a main script that enqueue and dequeue all of what is written on the inspector. Two more scripts, one script is a class for creating a new properties on the inspector where my friend can write, the other script functionality is to patch it on the inspector itself. I decided to add a patcher to customize unity editor to add a button. All what is left is a function to add another class where in my friend can write another character name and sentences.
This is what it looks like on Unity Inspector:
Please Help.
DialogueManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
private Queue<string> sentences;
public Text WrapperName;
public Text WrapperContent;
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
WrapperName.text = dialogue.name;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if(sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
WrapperContent.text = sentence;
}
public void EndDialogue()
{
Debug.Log("dialogue Ended..");
}
}
Dialogue.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3, 10)]
public string[] sentences;
}
StoryElement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StoryElement : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
elementscriptpatcher.cs
using System.Collections;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(StoryElement))]
public class elementscriptpatcher : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if(GUILayout.Button("Add another script"))
{
// I need to write a function for appending a class Dialogue on Dialogue.cs which was initialized on the StoryElement.
}
}
}
you can use GameObject.AddComponent.
if(GUILayout.Button("Add another script"))
{
gameObject.AddComponent<Dialogue>();
}
EDIT : As #derHugo said in comments, we can't add a class that's base is not Monobehaviour. So you will require an Access Class to access Dialogue.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AccessClass : MonoBehaviour {
public Dialogue mClassObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Use Ink for Unity, and author the dialogue as text files.

Custom Editor - Multi-object editing not supported

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.

Categories