Hi guys,.
I have been lurking around enjoying the Showcase forums, and have been pretty successful finding out what I need on the interwebz,…but I spent all weekend on this problem and cannot figure it out. I am hoping someone here can help!
First off,…C#.
I have a RigidBody prefab named 'newCard’. Attached with a C# script named ‘cardVars’.
Within ‘cardVars’ are two variables, an int count and a string cardName.
(ya’ll see where this is going don’t you? :p)
I have a ‘GameObject’ in my Hierarchy that holds my main script named, ‘generator’.
Within that script, I make a button that instantiates the preFab"newCard’.(declared ‘spawnCard’) at a specific location in the world; on a sphere named birthBall.
QUESTION: How on earth can I access the two variables in ‘cardVars.cs’ and change their values/string???
The following example runs,.but gives one error:
Unity3D berates you exclaiming:
preFab code ‘cardVars’ attached to ‘newCard’:
using UnityEngine;
using System.Collections;
public class cardVars: MonoBehaviour {
public string cardName;
public int count;
void Start () {
}
public void OnMouseEnter()
{
Debug.Log(" Name= " +cardName);
Debug.Log(" Count= " +count);
}
void Update () {
}
}
generator.cs Code attached to a GameObject in Hierarchy:
using UnityEngine;
using System.Collections;
public class generator : MonoBehaviour {
public Rigidbody spawnCard;
void Start () {
}
void OnGUI()
{
if (GUI.Button(new Rect(100, 300, 100, 100), "DealCard"))
{
Rigidbody newCardClone;
newCardClone =(Rigidbody)Instantiate(spawnCard,GameObject.Find("birthBall").transform.position, Quaternion.identity);
[COLOR="red"]cardVars myCardVars = newCardClone.GetComponent(typeof(cardVars)) as cardVars;
myCardVars.count = 33;
myCardVars.cardName = "IhazaFAIL";[/COLOR]
}
}
void Update () {
}
}
Runs fine,…click the button,…makes a newCard, although it appends the word ‘clone’ on everything, hence why I tried to use the name newCardClone when instantiating.
BUT
on mouse over none of my preFab’s variables have changed. and I cry and rageQuit and go see Pirates:On Stranger Tides…and cry some more.
I am not confident about the highlighted Red part at all, it’s been most troublesome!
Thank you all for reading this far, and any help will be most welcome!
-Welby
(the examples for this on the resource site confuses me even more!)
PS: this is just a sample of something bigger I am making. I ultimately want to use this to generate a sort of playing card, each with it’s own value( 1-10). Is instancing the way to go? or should I be looking for other ways.