- Home /
The question is answered, right answer was accepted
Can't create a Factory (Design Pattern)
Hello everyone,
I was here in this forum a while ago, and I'm learning Software Engineering at the moment. I'm using Unity3d in my spare time.
I created a game long before I started my degree, and while I'm working on it, I've learnt a lot of new things in coding, especially Design Pattern called "Business Layers" pattern.
Now I want to convert all my scripts that were in Javascript to C#.
I've created a Jet that flies, and I want to implement him my physics, through my C# solution, and not through JS. I'm creating a new "Unity C#" file in order to convert everything.
And here is the error I encountered, there is no namespace in the C# unity file, and somehow I can't create a new "FactoryPhysics" object or even use Inheritance.
Then how to solve this issue? how can I implement my Factory into the "Unity C#" file?
Edited question.
Here is the issue, I think it is explained enough:
This is the Factory script, which creates a singleton for my implemented interface.
using System;
using System.Collections;
namespace Physics
{
public class FactoryPhysics
{
static Physics.iPhysics p;
public static Physics.iPhysics getJetPhysics()
{
if(p == null)
p = new Physics.Physics_imp();
return p;
}
}
}
Now here is the Unity C# script:
using System;
using UnityEngine;
using System.Collections;
FactoryPhysics p = getJetPhysics(); //This line is the problem, explained below
public class Jet : MonoBehaviour
{
// Use this for initialization
void Start ()
{
rigidbody.AddRelativeForce(Vector3.forward * 200, ForceMode.Impulse);
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate()
{
}
}
The compiler says "Parser Error: Unexpected Symbol, FactoryPhysics." Although all the scripts are in the same solution.
Of which type is FactoryPhysics?
No offense in case I'm going to state the obvious, but:
All Unity objects need to derive from $$anonymous$$onoBehaviour
These object cannot have a Constructor, and cannot be created with "new"
Exclusively use AddComponent to create such an object via script
non-Untiy objects can be any type/class, and need a constructor
non-Unity-objects can be within your own namespace
Inheritance works for both type of objects
I believe support for namespaces has been changed/extended. Check the 4.x changelogs.
I think this sentence needs a lot clarification:
"I can't create a new "FactoryPhysics" object or even use Inheritance"
The thing is, that my FactoryObject, is actually an implemented interface, which I created so I can send information about values in the unity3D and receive values after changes I needed. The returned values are floats.
Both interfaces and abstract classes are possible, even for Unity objects. You'll have to explain some more, or give a code example of what doesn't work, and why it doesn't work.
Also, we still know nothing whatsoever what FactoryPhysics and FactoryObject are. Show us the class structure/framework/headers.
Answer by Wolfram · Feb 04, 2013 at 11:34 AM
Umh, add a "using Physics;" at the top, or use Physics.FactoryPhysics instead...?
Also, you are trying to call getJetPhysics outside of any class, and that line shouldn't be outside the class, too.
I added using, I changed also the namespace, to NewPhysics for if there might be an encounter with unity premade physics. the new line is:
FactoryPhysics p = FactoryPhysics.getJetPhysics();
and still, no change with the error.
Hmm, wait, I've did the other solution you offerred, to put the singleton inside the class. Now I receive this error: Cannot convert method group 'getJetPhysics' to non-delegate FactoryPhysics.
For the final time, I solved it thanks to you, I should've put the line inside the class and not outside. Now it works. thank you :)
the line is btw:
iPhysics p = FactoryPhysics.getJetPhysics();
As you can see in your code, getJetPhysics()
returns an object of type Physics.iPhysics
, but you are trying to assign it to Physics.FactoryPhysics
.
Ins$$anonymous$$d you should probably access your singleton as it is intended, by directly using Physics.FactoryPhysics.getJetPhysics()
each time you access it, ins$$anonymous$$d of storing your static global object in another local reference.
It could be that my design is still flawed.. since object p now stores information not only for functions, but also for variables. That's why I need to create it.
Follow this Question
Related Questions
Tropical Island level design 1 Answer
Reliable / intelligent user input handling 0 Answers
Using the Delegate Object design pattern in Unity 1 Answer
2D 360 degress platformer example needed 0 Answers
Creating a singleton game manager 1 Answer