I probably sound really stupid but I am having trouble checking if a gameobject has 2 different scripts in the start function can any one help with this?
GetComponent will return null if the component doesn’t exist.
It says it cannot covert [script name] to unityEngine.Gameobject.
http://pastebin.com/4Jq2YPT7 ← code
I have just realized that the script and game object have the same name and have now changed the object name but I’m still getting the same error.
You can add your code in your post using code tags.
public class BeeContoller : MonoBehaviour {
private GameObject flower1;
void Start () {
flower1 = GetComponent<flowerConrtoller>();
flower1 = GetComponent<flower1>();
}
}
- “flowerConrtoller” isn’t defined anywhere that I can see. And it’s likely spelled wrong as well.
- “flower1” is a variable name, not a component name.
Based on the questions you’re having I would suggest going through Unity’s scripting tutorials.
using UnityEngine;
using UnityEngine.UI;
public class BeeContoller : MonoBehaviour {
private flowerConrtoller Flower1;
void Start(){
Flower1 = GetComponent<flowerConrtoller>();
if (!Flower1.GetComponent<flower1>()) {
}
}
could I use this and if so what would I put in the if statment
Replace your if statement with a simple if statement that checks if Flower1 == null
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class someScript : MonoBehaviour {
//define flower1
//define flowerController
private GameObject obj;
void Start () {
if(GetComponent<flowerConrtoller>())
assign flowerController to your variable
if(GetComponent<flower1>())
assign flower1 to your variable
}
}
GetComponent will return a true statement if the component is not null/actually exists.
Of course if you're going to have many of the same object and they'll all have the same functionality it might be better to use a prefab.