Change Sprite array to GameObject array in Unity - c#

[EDIT]: current inspector output image
I'm beginning with unity and I want to convert this public array of sprites into a public array of gameobjects. How would I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour, IRecyle
{
public Sprite[] sprites;
public void Restart()
{
var renderer = GetComponent<SpriteRenderer>();
renderer.sprite = sprites[Random.Range(0, sprites.Length)];
}
public void Shutdown()
{
}
}

if you have prefabs with the sprites you want to use try to change your code:
this:
public Sprite[] sprites;
to this:
public GameObject[] spriteGameObjects;`
and also this:
renderer.sprite = sprites[Random.Range(0, sprites.Length)];
to this:
renderer.sprite = spritesGameObjects[Random.Range(0, spritesGameObjects.Length)]
.GetComponent<SpriteRenderer>().sprite;
I don't have access to Unity right now, so I haven't checked if it's working, but I think it should.
Kindly let me know if this helps or not.

Related

C# errror "CS0116: A namespace cannot directly contain members such as fields or methods"

I get an error, Assets\scripts\Skins.cs(9,12): error CS0116: A namespace cannot directly contain members such as fields or methods on the Internet they write what it means incorrectly placed brackets, but I don’t see any errors
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public GameObject choise1;
public GameObject choise2;
public GameObject choise3;
public int skin = 1;
public class Skins : MonoBehaviour {
void Choise () {
if (Input.GetMouseDown(choise1)){
choise2.SetActive (false);
}
}
}
GameObject and that int skin need to be under Skins class, like Choise
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Skins : MonoBehaviour {
public GameObject choise1;
public GameObject choise2;
public GameObject choise3;
public int skin = 1;
void Choise () {
if (Input.GetMouseDown(choise1)){
choise2.SetActive (false);
}
}
}
If you still have some problems here, because I don't know what are u trying to do with that choiseX, maybe you need to create them.
I mean, maybe u are trying to write something like this
void Choise () {
var GameObject choise1 = new GameObject();
var GameObject choise2 = new GameObject();
var GameObject choise3 = new GameObject();
int skin = 1;
if (Input.GetMouseDown(choise1)){
choise2.SetActive (false);
}
}

Instantiate scriptableobject with random variables

I just started to work with scriptableobject, and I'm trying to Instantiate / Create Instance of a very basic scriptableobject that got 4 line of arrays.
I'm trying to Instantiate one scriptableobject that I created, that every time he's been created he will randomize his variables
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Character",menuName = "Character Creation/Player Units")]
public class CharStats : ScriptableObject
{
[SerializeField]
public string [] charName;
public string [] charDescription;
public string [] charSin;
public Sprite [] charIcon;
}
The changing I'm doing do inside the inspector for one scriptableobject, and just his inside variables will change every time I create new one. Is it possible to achieve?
Ok so I manage to create something might be not the right way, but after pressing a key ( for now testing) its Instantiate my scriptableobjectwith a random Icon I put on for now. Ill add the 3 scripts, bad names coz it's for testing only, (testing - its script that sitting on empty gameobject with reference to my scriptableobject, testing 2 - its script that sitting on a prefab that I Instantiate with the scriptableobject)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class testing2 : MonoBehaviour
{
//public ScriptableObject person;
public CharStats charSats;
public Sprite sprite2;
private SpriteRenderer spriteRender;
private void Start()
{
spriteRender = GetComponent<SpriteRenderer>();
GetRandomSprite();
}
public Sprite GetRandomSprite()
{
int randomIndex = Random.Range(0, charSats.charIcon.Length);
Debug.Log(charSats.charIcon[randomIndex]);
if (sprite2 == null)
{
sprite2 = charSats.charIcon[randomIndex];
spriteRender.sprite = sprite2;
}
return charSats.charIcon[randomIndex];
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class Testing : MonoBehaviour
{
public GameObject Player;
private void Update()
{
SpawnPlayer();
}
public void SpawnPlayer()
{
if (Input.GetKeyDown(KeyCode.A))
{
GameObject spawnPalyer = Instantiate(Player, new Vector3(Random.Range(-10,10), Random.Range(-10, 10)), Quaternion.identity) as GameObject;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Character",menuName = "Character Creation/Player Units")]
public class CharStats : ScriptableObject
{
[SerializeField]
public string [] charName;
public string [] charDescription;
public string [] charSin;
public Sprite [] charIcon;
}

How to properly make variable Public so it can be accessed by another script?

In Unity I have 2 GameObjects, a sphere and a capsule.
And I have a script attached to each.
Capsule script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh capsuleMesh;
void Awake()
{
capsuleMesh = GetComponent<MeshFilter>().mesh;
Debug.Log(capsuleMesh);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Sphere script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
Mesh mesh;
void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
Debug.Log(mesh);
}
// Start is called before the first frame update
void Start()
{
mesh = capsuleMesh;
}
// Update is called once per frame
void Update()
{
}
}
The mesh = capsuleMesh here is giving me an error about "the name capsuleMesh does not exist in the current context".
I thought that making capsuleMesh public in the other script would make THIS script be able to access it without issue.
What am I doing wrong?
capsuleMesh is a class variable defined in the CapsuleMesh class. It's not a global variable you can use everywhere. You need a reference to the instance of the CapsuleMesh class to retrieve the mesh stored in the capsuleMesh variable.
I've reworked your both scripts to make them work. I've spotted a flaw in your scripts. I guess ChangeMesh is meant to change the mesh of the gameObject? If so, you need to assign a new value to the meshFilter.mesh. Assigning a new reference to the mesh class variable is not enough (it would be pretty long to explain why)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh Mesh
{
get ; private set;
}
void Awake()
{
Mesh = GetComponent<MeshFilter>().mesh;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
// Drag & drop in the inspector the gameObject holding the `CapsuleMesh` component
public CapsuleMesh CapsuleMesh;
private MeshFilter meshFilter;
void Awake()
{
meshFilter = GetComponent<MeshFilter>();
}
void Start()
{
meshFilter.mesh = CapsuleMesh.Mesh;
}
}

How i use array of vector3 to list in inspector all vector3 in list?

In the top of script:
public Vector3[] positionsList;
List<Vector3> positions = new List<Vector3>();
In Update: I'm updating the List but i also want to update the array so i can watch the vector3 positions in the inspector while the game is running.
var position = GenerateRandomPositions(objects[0]);
if (!positions.Contains(position))
{
positions.Add(position);
}
Simply make the List public, the inspector can handle it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ListInInspectorTest : MonoBehaviour {
public List<Vector3> superawesomeListOfVector3s;
void Update () {
if(superawesomeListOfVector3s.Count < 10) {
superawesomeListOfVector3s.Add(Random.insideUnitSphere);
}
}
}
If it is important that the variable is private or protected to other code. you could add the [SerializeField] tag above the variable that you want to see in the inspector. source (https://unity3d.com/learn/tutorials/topics/tips/show-private-variables-inspector)

Instantiating a list of gameobjects in Unity C#

How can I instantiate a list of GameObject in Unity3D using c#?
I fill the list with prefabs manually in inspector window.
Below is the code I've written in Deck.cs, but I get "Object reference is not set to an instance of an object". If you have a solution with an array, that will be appreciated as well.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Assets
{
class Deck:MonoBehaviour
{
public List<GameObject> deck;
public void Fill()
{
GameObject c1 = Instantiate(deck[0]);
GameObject c2 = Instantiate(deck[1]);
GameObject c3 = Instantiate(deck[2]);
GameObject c4 = Instantiate(deck[3]);
GameObject c5 = Instantiate(deck[4]);
GameObject c6 = Instantiate(deck[5]);
}
}
}
I also tried doing it with an array and I get 'The object you want to instantiate is null'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Assets
{
class Deck:MonoBehaviour
{
public GameObject card1;
public GameObject card2;
public GameObject card3;
public GameObject card4;
public GameObject card5;
public GameObject card6;
public GameObject[] deck;
public void Start ()
{
deck = new GameObject[5];
GameObject c1 = Instantiate(card1) as GameObject;
GameObject c2 = Instantiate(card2) as GameObject;
GameObject c3 = Instantiate(card3) as GameObject;
GameObject c4 = Instantiate(card4) as GameObject;
GameObject c5 = Instantiate(card5) as GameObject;
GameObject c6 = Instantiate(card6) as GameObject;
}
}
}
Well considering your comments and that the first script your provided works perfectly without any error (as it should: there's no reason it should return an error), I feel like you are very new to Unity.
Therefore I'd recommend you to look to the Unity tutorials before anything else (they are quite well made and will help you understand the basics of the engine). You can find the Roll-a-ball tutorial here.
About your question:
1- In the first script, the Fill() method isn't called anywhere, you have to do something like this:
private void Start()
{
Fill();
}
2- This Start() method comes from the MonoBehaviour parent class (as well as the Update() method which is called every frame) and is called once at the beginning of the scene. Look to this chart to understand how unity flow is made.
3- Using a List<GameObject> or a GameObject[] all depends on your situation: if the size of the collection is going to change, go for a list. Otherwise array is advised.
4- Considering your script my guess is it should look like this:
namespace Assets
{
class Deck : MonoBehaviour
{
[SerializeField]
private GameObject[] deck;
private GameObject[] instanciatedObjects;
private void Start()
{
Fill();
}
public void Fill()
{
instanciatedObjects = new GameObject[deck.Length];
for (int i = 0; i < deck.Lenght; i++)
{
instanciatedObjects[i] = Instanciate(deck[i]) as GameObject;
}
}
}
}
You can of course still use lists if needed :)
EDIT:
If you want to use a list you simply have to:
change private GameObject[] instanciatedObjects; to private List<GameObject> instanciatedObjects;
replace instanciatedObjects = new GameObject[deck.Length]; by instanciatedObjects = new List<GameObject>();
replace instanciatedObjects[i] = Instanciated(deck[i]) as GameObject; by instanciateObjects.Add(Instanciated(deck[i]) as GameObject);
Hope this helps,
Use foreach to loop over your List with prefabs, like this:
public List<GameObject> Deck = new List<GameObject>(); // im assuming Deck is your list with prefabs?
public List<GameObject> CreatedCards = new List<GameObject>();
void Start()
{
Fill();
}
public void Fill()
{
foreach(GameObject _go in Deck)
{
GameObject _newCard = (GameObject)Instantiate(_go);
CreatedCards.Add(_newCard);
}
}
Also it's a good habit to name your lists with capital letter.

Categories