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
Related
I just started game development and got into a problem
whenever I load my Visual Studio code into Unity it says
The refrenced script unknown on this behaviour is missing
This is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VirtualManMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Hello, World!") ;
}
// Update is called once per frame
void Update()
{
}
}
I need a solution for this
This may happen is the file name does not match the class name. Make sure that the file containing this script is also named VirtualManMovement.cs
Make sure in Unity you go to edit -> preferences and see that you have Unity External tools in Your IDE Selected.
Ensure your Code File Name is the same as your Class to prevent confusion.
I was confused as well when I first started. Hopefully, you find this useful
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
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);
}
}
So apparently i suck at listening at my university, because i can't figure this out, not even with google...
How do you create a scriptable object in the editor? I have the project open, it looks like this:
Click the Create button as if you wanted to create a folder or C# script or anything.
Select the ScriptableObject from the popup menu.
Get this panel and finalize the object after selecting the script for it.
The problem is: i don't have the ScriptableObject button. I have a script that is a scriptable object (to make sure i even copied the one from the project of the university). I restarted Unity, i checked if there were any packages installed (there werent) and i googled quite a bit. But i just can't seem to get this working...
Is there anything i have to install or add first?
Thanks in advance!
You need another script to add the button which will create an instance from that scriptable object. something like that
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MakeScriptableObject {
[MenuItem("Assets/Create/My Scriptable Object")]
public static void CreateMyAsset()
{
MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();
AssetDatabase.CreateAsset(asset, "Assets/NewScripableObject.asset");
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}
You can check this Introduction to Scriptable Objects tutorial on unity website.
I can't comment so just place it like answer:
Don't forget to use UnityEditor.AssetDatabase.GenerateUniqueAssetPath coz simple AssetDatabase.CreateAsset can erase your data:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MakeScriptableObject
{
[MenuItem("Assets/Create/My Scriptable Object")]
public static void CreateMyAsset()
{
MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();
string name = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/NewScripableObject.asset");
AssetDatabase.CreateAsset(asset, name);
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}