I'm trying to do a script that when the player pass by a item, it activates it's effect in the player.
But this error apears:
This is my code:
`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Bonus : MonoBehaviour
{
public string name;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Player"))
{
collision.SendMessage(name, null, SendMessageOptions.RequireReceiver);
Destroy(gameObject);
}
}
}
`
I already tried adding the
SendMessageOptions.RequireReceiver
but it didn't work
What the error is trying to tell you is:
On your collision's GameObject there is no component attached that implements a method called whatever the value of name is (in your case doubleSpeed).
By default the option already is SendMessageOptions.RequireReceiver, if there is none you get that error message.
The receiving method can choose to ignore the argument by having zero parameters. If options is set to SendMessageOptions.RequireReceiver an error is printed if the message is not picked up by any component.
If you want to ignore the case where there is no receiver you would rather pass in SendMessageOptions.DontRequireReceiver
In general instead of using SendMessage at all rather get your component and call the method directly
Related
I am completely new to Unity and all other answers I've found for this go over my head.
So far I've run everything from the same script, which is getting very big and messy. Therefore I am trying to learn how to call methods from other scripts.
I have a dropdown menu with the code in one script and I'm trying to call that code from another.
ScriptA:
using UnityEngine;
public class ChoseLanguage: MonoBehaviour
{
public TMPro.TMP_Dropdown myDrop;
DisplayController displayController;
public void DropdownChooseLanguage()
{
if (myDrop.value == 1)
PlayerPrefs.SetString("chosenLanguage", "Spanish");
if (myDrop.value == 2)
PlayerPrefs.SetString("chosenLanguage", "Japanese");
if (myDrop.value == 3)
PlayerPrefs.SetString("chosenLanguage", "Korean");
if (myDrop.value == 4)
PlayerPrefs.SetString("chosenLanguage", "Icelandic");
Debug.Log(PlayerPrefs.GetString("chosenLanguage"));
displayController.DropdownSetLanguage();
}
}
The selection code works by itself, and the debug.Log shows that the chosen language is being saved correctly to PlayerPrefs.
The error comes when it tries to read the "displayController.DropdownChooseLanguage();" line. (Line 28)
Unity gives this error:
NullReferenceException: Object reference not set to an instance of an object
ChoseLanguage.DropdownChooseLanguage () (at Assets/Scripts/ChoseLanguage.cs:28)
Script B
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using TMPro;
public class DisplayController : MonoBehaviour
{
...
public void DropdownSetLanguage()
{
SetFileName();
setLanguage.gameObject.SetActive(false);
Start();
}
...
}
Earlier, the exact same code from Script A was placed in ScriptB and all the code worked as it should.
This is a very noob question but I have simply just never been able to understand how exactly to access other scripts correctly.
Any help will be greatly appreciated.
Thanks.
EDIT:
I found a solution to this but I'll keep the question up in case other beginners have the same problem or if anyone has a better solution.
I made the DisplayController displayController; into public DisplayController displayController; and then dragged the gameobject with the displaycontroller script attached into the slot for it.
DropdownSetLanguage is public and it seems you think you can access it by
creating a DisplayController object named displayController.
It is ok
about to access a class but, displayController should be null in this state.
Because you have to assign an initial DisplayController to displayController
in your first code.
You can add public or [Serializable] tag in front of
DisplayController displayController; in your first code then, you can add
a GameObject which has a DisplayController script in it in Unity editor.
accessibility
I have recently been following a tutorial series on Procedurally Generated world generation, I am fairly new to coding and I have come across the following problem. I have 3 error messages in my code,
The first error says:
Error CS0115 'MapGeneratorEditor.OnInspectorGUI()': no suitable method found to override
The second says:
Error CS0103 The name 'target' does not exist in the current context
And the third error is:
Error CS0103 The name 'DrawDefaultInspector' does not exist in the current context
I think that these are somehow related to each other
I am using Unity 2019.4.16f1 personal and here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class MapGeneratorEditor : MonoBehaviour
{
public override void OnInspectorGUI()
{
MapGenerator mapGen = (MapGenerator)target;
DrawDefaultInspector();
if(GUILayout.Button("Generate"))
{
mapGen.GenerateMap();
}
}
}
You are inheriting your MapGeneratorEditor class from MonoBehavior, but OnInspectorGUI function only member of Editor class. You should replace MonoBehavior with Editor
MapGeneratorEditor:Editor
here is referances where your functions comes
OnInspectorGUI
DrawDefaultInspector
Why all my newly created script always have this error? Even though i had put in my intended code, all my script always show like this.
my code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Testing : MonoBehaviour
{
public GameObject obj;
// Start is called before the first frame update
void Start()
{
obj = GetComponent<GameObject>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
collision.transform.position = obj.transform.position;
}
}
Welcome to Stackoverflow!
This issue is mostly caused when you create a script outside the Unity Editor, i.e. in your IDE or editor of choice. Whenever you want to create a script, you need to create it inside the Editor, by right-clicking in your project folder and selecting "New C# Script".
If you do create your script inside Unity, you need to make sure that the class name (in your case, "Testing") is the same as the script file name.
Regardless of whether these solutions work for you, please make sure to search for answers before posting, as there are multiple duplicate issues in the Unity forums.
I have this script that allows me to change to a different scene when I push a button. I've used it multiple times before with no issue, but recently I've been getting this compiler error that's telling me the script can't be accessed due to its protect level. I did some research and it supposedly means something is set to private instead of public, but everything in my script is public to begin with. Please help? According to Unity, the error is the ".LoadScene" inside public void DoSceneChange().
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChange : MonoBehaviour
{
[SerializeField]
string levelToLoad;
public void DoSceneChange()
{
SceneManager.LoadScene(levelToLoad);
}
}
As the title says, I'm trying to set up a UI slider so the player can adjust some of the post processing settings (specifically the exposure and temperature) while the game is running.
To bring you up to speed:
I'm using version one of the post processing stack, as found here:
https://assetstore.unity.com/packages/essentials/post-processing-stack-83912
I'm very new to C#; I'm at the "Frankenstein" stage of learning how to code (following tutorials and modifying what works until it breaks or does what I'm trying to accomplish).
I figure my best shot is to try to adapt what I learned from this tutorial on creating an audio volume slider: https://www.youtube.com/watch?v=YOaYQrN1oYQ&t=122s
This is the code I've cobbled together so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BrightnessSlider : MonoBehaviour {
public void SetBrightness (float brightness)
{
Debug.Log(brightness);
}
}
Specific issues I'm having at specific points in the tutorial:
2:07 For the function of the slider, the tutorial sets up a dynamic float that matches the custom method (SetVolume) they specify. When I try setting up my own function with a custom method (SetBrightness), I can't find it. I'm also not sure if I need to set a different object instead of the canvas for this step.
3:47 In the tutorial they expose a parameter for the volume so that it can be manipulated through script, but I don't know what the equivalent of that would be for post processing.
For the record, I was able to follow this tutorial to create my own audio slider and got it working with no problems.
One last thing: I opened up the script for the post processing profile and found the variable type I think I'd need or would at least be somewhat relevant: ColorGradingModel, but I honestly have no idea what to do with this information.
Update July 09, 2018
I've since been looking over #Nol's code and had someone else look at it and help me out with it. At the moment, the slider's functionality(not sure if that's the correct terminology but that's what I've been sticking with) is set up through the On Value Changed field in the inspector , but it's not actually driving/changing the brightness values. I had someone else (who's far more qualified than I am) look at it with me. It seems like it should work the way they've set it up, but something is getting lost in translation between the method and the slider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
using UnityEngine.UI;
public class BrightnessSlider : MonoBehaviour
{
public Slider slider;
public PostProcessingProfile Default;
private ColorGradingModel cgm;
private void Start()
{
//I haven't been able to get this to not return some sort of error,
//and I'm not even sure of its usefulness.
//I've been keeping it commented out for the most part.
Default.profile.TryGetSettings(out cgm);
}
public void SetBrightness(float brightness)
{
ColorGradingModel.Settings settings = cgm.settings;
settings.basic.postExposure = brightness;
cgm.settings = settings;
Debug.Log("Brightness is: " + brightness); //For testing purposes
}
}
It seems like you have the core of what you need to know for changing your settings down. That's good.
You'll need a few simple changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing; //How you'll access PPV (Post Processing Volume) models and settings
public class BrightnessSlider : MonoBehaviour {
PostProcessingVolume ppv; //You can make this public to set in inspector
ColorGradingModel cgm; //can use ppv.profile.TryGetSettings(out cgm) in Start()
public void SetBrightness (float brightness)
{
cg.[setting you want to change].value = brightness;
}
}