The question is answered, right answer was accepted
The name `FindObjectOfType' does not exist in the current context
I'm attempting to reference a public variable of one script within another by using
FindObjectOfType ScriptName (); (for some reason the little carrots make "ScriptName" dissappear but the syntax in the code is correct)
and I'm receiving this error. Can anyone tell me:
A) Why this is happening?
B) How to fix it?
C) A better way to achieve what I'm trying to do?
Have you added the UnityEngine namespace ?
Please, provide the full script an use the 101010 button to format your code
Answer by Taylor-Libonati · May 27, 2016 at 04:51 PM
It means you are calling a function that doesn't exist.
The "FindObjectOfType" method is a static function for Objects meaning you don't call "gameObject.FindObjectOfType" you instead call "GameObject.FindObjectOfType".
Where "GameObject" is the class, "gameObject" is an instance of the GameObject class and is a variable of MonoBehaviours.
In your case, since your class doesn't inherit from MonoBehaviour or Object you don't have a function called FindObjectOfType so you need to call the static function of something that does ie GameObject.
Proper use of FindObjectOfType with your own class:
SomeClass myObject = GameObject.FindObjectOfType<SomeClass>();
Answer by heky · May 27, 2016 at 05:48 PM
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public static class Noise {
public enum NormalizeMode {Local, Global};
public static float[,] GenerateNoiseMap(stuff) {
MapGenerator generator = FindObjectOfType<MapGenerator>();
The rest of the code is irrelevant. MapGenerator is the name of another script I'm attempting to pull.
Follow this Question
Related Questions
Getting Int value from script to other script 2 Answers
The referenced script on this Behaviour is missing - on any new script 0 Answers
Instatiated object not being referenced in Start function? 2 Answers
Score isn't able to be counted 0 Answers
Referencing instances of scripts based on the GameObject they are attached to. 0 Answers